Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='test git rev-parse --parseopt' |
| 4 | . ./test-lib.sh |
| 5 | |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 6 | cat > expect <<\END_EXPECT |
| 7 | cat <<\EOF |
Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 8 | usage: some-command [options] <args>... |
Jeff King | 44d86e9 | 2008-06-14 03:27:21 -0400 | [diff] [blame] | 9 | |
Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 10 | some-command does foo and bar! |
| 11 | |
| 12 | -h, --help show the help |
| 13 | --foo some nifty option --foo |
| 14 | --bar ... some cool option --bar with an argument |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 15 | -b, --baz a short and long option |
Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 16 | |
| 17 | An option group Header |
Michele Ballabio | 6422f63 | 2008-06-22 16:39:04 +0200 | [diff] [blame] | 18 | -C[...] option C with an optional argument |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 19 | -d, --data[=...] short and long option with an optional argument |
Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 20 | |
| 21 | Extras |
| 22 | --extra1 line above used to cause a segfault but no longer does |
| 23 | |
| 24 | EOF |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 25 | END_EXPECT |
Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 26 | |
Uwe Kleine-König | 824af25 | 2009-06-14 01:58:42 +0200 | [diff] [blame] | 27 | cat > optionspec << EOF |
Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 28 | some-command [options] <args>... |
| 29 | |
| 30 | some-command does foo and bar! |
| 31 | -- |
| 32 | h,help show the help |
| 33 | |
| 34 | foo some nifty option --foo |
| 35 | bar= some cool option --bar with an argument |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 36 | b,baz a short and long option |
Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 37 | |
| 38 | An option group Header |
| 39 | C? option C with an optional argument |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 40 | d,data? short and long option with an optional argument |
Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 41 | |
| 42 | Extras |
| 43 | extra1 line above used to cause a segfault but no longer does |
| 44 | EOF |
Uwe Kleine-König | 824af25 | 2009-06-14 01:58:42 +0200 | [diff] [blame] | 45 | |
| 46 | test_expect_success 'test --parseopt help output' ' |
Jonathan Nieder | 2b5ec01 | 2010-10-31 02:35:49 -0500 | [diff] [blame] | 47 | test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec && |
Jiang Xin | 9a00138 | 2012-08-27 13:36:55 +0800 | [diff] [blame] | 48 | test_i18ncmp expect output |
Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 49 | ' |
| 50 | |
Uwe Kleine-König | 824af25 | 2009-06-14 01:58:42 +0200 | [diff] [blame] | 51 | cat > expect <<EOF |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 52 | set -- --foo --bar 'ham' -b -- 'arg' |
Uwe Kleine-König | 824af25 | 2009-06-14 01:58:42 +0200 | [diff] [blame] | 53 | EOF |
| 54 | |
| 55 | test_expect_success 'test --parseopt' ' |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 56 | git rev-parse --parseopt -- --foo --bar=ham --baz arg < optionspec > output && |
Uwe Kleine-König | 824af25 | 2009-06-14 01:58:42 +0200 | [diff] [blame] | 57 | test_cmp expect output |
| 58 | ' |
| 59 | |
| 60 | test_expect_success 'test --parseopt with mixed options and arguments' ' |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 61 | git rev-parse --parseopt -- --foo arg --bar=ham --baz < optionspec > output && |
Uwe Kleine-König | 824af25 | 2009-06-14 01:58:42 +0200 | [diff] [blame] | 62 | test_cmp expect output |
| 63 | ' |
| 64 | |
| 65 | cat > expect <<EOF |
| 66 | set -- --foo -- 'arg' '--bar=ham' |
| 67 | EOF |
| 68 | |
| 69 | test_expect_success 'test --parseopt with --' ' |
| 70 | git rev-parse --parseopt -- --foo -- arg --bar=ham < optionspec > output && |
| 71 | test_cmp expect output |
| 72 | ' |
| 73 | |
Uwe Kleine-König | 6e0800e | 2009-06-14 01:58:43 +0200 | [diff] [blame] | 74 | test_expect_success 'test --parseopt --stop-at-non-option' ' |
| 75 | git rev-parse --parseopt --stop-at-non-option -- --foo arg --bar=ham < optionspec > output && |
| 76 | test_cmp expect output |
| 77 | ' |
| 78 | |
Uwe Kleine-König | 824af25 | 2009-06-14 01:58:42 +0200 | [diff] [blame] | 79 | cat > expect <<EOF |
| 80 | set -- --foo -- '--' 'arg' '--bar=ham' |
| 81 | EOF |
| 82 | |
| 83 | test_expect_success 'test --parseopt --keep-dashdash' ' |
| 84 | git rev-parse --parseopt --keep-dashdash -- --foo -- arg --bar=ham < optionspec > output && |
| 85 | test_cmp expect output |
| 86 | ' |
| 87 | |
Uwe Kleine-König | 2998138 | 2010-07-06 16:46:05 +0200 | [diff] [blame] | 88 | cat >expect <<EOF |
| 89 | set -- --foo -- '--' 'arg' '--spam=ham' |
| 90 | EOF |
| 91 | |
| 92 | test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option with --' ' |
| 93 | git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo -- arg --spam=ham <optionspec >output && |
| 94 | test_cmp expect output |
| 95 | ' |
| 96 | |
| 97 | cat > expect <<EOF |
| 98 | set -- --foo -- 'arg' '--spam=ham' |
| 99 | EOF |
| 100 | |
| 101 | test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option without --' ' |
| 102 | git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo arg --spam=ham <optionspec >output && |
| 103 | test_cmp expect output |
| 104 | ' |
| 105 | |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 106 | cat > expect <<EOF |
| 107 | set -- --foo --bar='z' --baz -C'Z' --data='A' -- 'arg' |
| 108 | EOF |
| 109 | |
| 110 | test_expect_success 'test --parseopt --stuck-long' ' |
| 111 | git rev-parse --parseopt --stuck-long -- --foo --bar=z -b arg -CZ -dA <optionspec >output && |
| 112 | test_cmp expect output |
| 113 | ' |
| 114 | |
| 115 | cat > expect <<EOF |
| 116 | set -- --data='' -C --baz -- 'arg' |
| 117 | EOF |
| 118 | |
| 119 | test_expect_success 'test --parseopt --stuck-long and empty optional argument' ' |
| 120 | git rev-parse --parseopt --stuck-long -- --data= arg -C -b <optionspec >output && |
| 121 | test_cmp expect output |
| 122 | ' |
| 123 | |
| 124 | cat > expect <<EOF |
| 125 | set -- --data --baz -- 'arg' |
| 126 | EOF |
| 127 | |
| 128 | test_expect_success 'test --parseopt --stuck-long and long option with unset optional argument' ' |
| 129 | git rev-parse --parseopt --stuck-long -- --data arg -b <optionspec >output && |
| 130 | test_cmp expect output |
| 131 | ' |
| 132 | |
| 133 | test_expect_success 'test --parseopt --stuck-long and short option with unset optional argument' ' |
| 134 | git rev-parse --parseopt --stuck-long -- -d arg -b <optionspec >output && |
| 135 | test_cmp expect output |
| 136 | ' |
| 137 | |
Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 138 | test_done |