Merge lp:~dave-cheney/gnuflag/001-parse-more-gnu-args into lp:~gophers/gnuflag/trunk

Proposed by Dave Cheney
Status: Needs review
Proposed branch: lp:~dave-cheney/gnuflag/001-parse-more-gnu-args
Merge into: lp:~gophers/gnuflag/trunk
Diff against target: 66 lines (+12/-4)
2 files modified
flag.go (+6/-2)
flag_test.go (+6/-2)
To merge this branch: bzr merge lp:~dave-cheney/gnuflag/001-parse-more-gnu-args
Reviewer Review Type Date Requested Status
The Go Language Gophers Pending
Review via email: mp+127419@code.launchpad.net

Description of the change

add --format=json style gnu args parsing

https://codereview.appspot.com/6588060/

To post a comment you must log in.
Revision history for this message
Dave Cheney (dave-cheney) wrote :

Please take a look.

13. By Dave Cheney

moar test

Revision history for this message
Roger Peppe (rogpeppe) wrote :

thanks for working on this - it needed doing!

a few thoughts below.

https://codereview.appspot.com/6588060/diff/1/flag.go
File flag.go (right):

https://codereview.appspot.com/6588060/diff/1/flag.go#newcode729
flag.go:729: if len(v) > 1 && len(v[1]) > 0 {
this seems a bit odd to me. i think it's perfectly ok to have an empty
*value*, but an empty flag is wrong.

i think we need to return an error here if there's an empty flag. if we
store the entire string following the flag (including the leading '=')
inside f.procFlag, then parseGnuFlagArg can know that it's got that
style of flag, and process accordingly.

that way we allow the following cases:
--foo=false
-x=true
--bar=

but not
--=x

i think -= should probably be an error too.

https://codereview.appspot.com/6588060/diff/1/flag_test.go
File flag_test.go (right):

https://codereview.appspot.com/6588060/diff/1/flag_test.go#newcode127
flag_test.go:127: "--format=json",
more test cases here please.

https://codereview.appspot.com/6588060/

Revision history for this message
Roger Peppe (rogpeppe) wrote :

See alternative CL at https://codereview.appspot.com/6598052/

On 2 October 2012 12:15, <email address hidden> wrote:
> Thank you for your comments. I have a slight worry about scope creep, we
> only have to support --format=json to fix the current bug in juju.
>
>
>
> https://codereview.appspot.com/6588060/diff/1/flag.go
> File flag.go (right):
>
> https://codereview.appspot.com/6588060/diff/1/flag.go#newcode729
> flag.go:729: if len(v) > 1 && len(v[1]) > 0 {
> On 2012/10/02 07:10:01, rog wrote:
>>
>> this seems a bit odd to me. i think it's perfectly ok to have an empty
>
> *value*,
>>
>> but an empty flag is wrong.
>
>
>> i think we need to return an error here if there's an empty flag. if
>
> we store
>>
>> the entire string following the flag (including the leading '=')
>
> inside
>>
>> f.procFlag, then parseGnuFlagArg can know that it's got that style of
>
> flag, and
>>
>> process accordingly.
>
>
>> that way we allow the following cases:
>> --foo=false
>
>
> yup, will fix.
>
>> -x=true
>
>
> I don't think this is valid gnuarg

Hmm, that's a good point. I should disallow = for single-letter flags.

>> --bar=
>
>
>> but not
>> --=x
>
>
> good point, this has no name.
>
>
>> i think -= should probably be an error too.
>
>
> I think it is an error in the same way -x=true is an error

actually, -x=true is not necessarily an error with GNU flag parsing - it
specifies that -x has the argument "=true".

-=true is always an error though, as is -=.

Unmerged revisions

13. By Dave Cheney

moar test

12. By Dave Cheney

support --format=json style gnu args

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'flag.go'
--- flag.go 2012-02-02 11:25:56 +0000
+++ flag.go 2012-10-02 06:59:20 +0000
@@ -35,6 +35,7 @@
35 "os"35 "os"
36 "sort"36 "sort"
37 "strconv"37 "strconv"
38 "strings"
38 "time"39 "time"
39 "unicode/utf8"40 "unicode/utf8"
40)41)
@@ -723,8 +724,11 @@
723724
724 // long flag signified with "--" prefix725 // long flag signified with "--" prefix
725 if a[1] == '-' {726 if a[1] == '-' {
726 // TODO allow '=' so we can set bools to false?727 v := strings.SplitN(a[2:], "=", 2)
727 flagName = a[2:]728 flagName = v[0]
729 if len(v) > 1 && len(v[1]) > 0 {
730 f.procFlag = v[1]
731 }
728 f.procArgs = f.procArgs[1:]732 f.procArgs = f.procArgs[1:]
729 return733 return
730 }734 }
731735
=== modified file 'flag_test.go'
--- flag_test.go 2012-02-02 11:13:43 +0000
+++ flag_test.go 2012-10-02 06:59:20 +0000
@@ -119,10 +119,12 @@
119 []string{119 []string{
120 "--bool2",120 "--bool2",
121 "--int", "22",121 "--int", "22",
122 "--int2=9000",
122 "--int64", "0x23",123 "--int64", "0x23",
123 "--uint", "24",124 "--uint", "24",
124 "--uint64", "25",125 "--uint64", "25",
125 "--string", "hello",126 "--string", "hello",
127 "--format=json",
126 "--float64", "2718e28",128 "--float64", "2718e28",
127 "--duration", "2m",129 "--duration", "2m",
128 "one - extra - argument",130 "one - extra - argument",
@@ -131,10 +133,12 @@
131 "bool": false,133 "bool": false,
132 "bool2": true,134 "bool2": true,
133 "int": 22,135 "int": 22,
136 "int2": 9000,
134 "int64": int64(0x23),137 "int64": int64(0x23),
135 "uint": uint(24),138 "uint": uint(24),
136 "uint64": uint64(25),139 "uint64": uint64(25),
137 "string": "hello",140 "string": "hello",
141 "format": "json",
138 "float64": 2718e28,142 "float64": 2718e28,
139 "duration": 2 * 60 * time.Second,143 "duration": 2 * 60 * time.Second,
140 },144 },
@@ -369,8 +373,8 @@
369 var buf bytes.Buffer373 var buf bytes.Buffer
370 flags.SetOutput(&buf)374 flags.SetOutput(&buf)
371 flags.Init("test", ContinueOnError)375 flags.Init("test", ContinueOnError)
372 flags.Parse(true, []string{"-unknown"})376 flags.Parse(true, []string{"--unknown"})
373 if out := buf.String(); !strings.Contains(out, "-unknown") {377 if out := buf.String(); !strings.Contains(out, "unknown") {
374 t.Logf("expected output mentioning unknown; got %q", out)378 t.Logf("expected output mentioning unknown; got %q", out)
375 }379 }
376}380}

Subscribers

People subscribed via source and target branches