You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+32-6Lines changed: 32 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -643,7 +643,7 @@ source `start --completion`
643
643
644
644
Completion will register to your shell by your program name, so you `MUST` give your program a fix name
645
645
646
-
#### 13. Hide Entry [ >= 1.3.0]
646
+
#### 13. Hide Entry [ >= 1.3 ]
647
647
648
648
Sometimes, you want to hide some entry from user, because they should not see or are not necessary to know the entry, but you can still use the entry. Situations like:
649
649
@@ -685,7 +685,7 @@ Which will have effect on `Shell Completion Script`
685
685
686
686
[example](examples/hide-help-entry/main.go)
687
687
688
-
#### 14. Invoked & InvokeAction [ >= 1.4.0]
688
+
#### 14. Invoked & InvokeAction [ >= 1.4 ]
689
689
690
690
When there is valid match for main parser or sub parser, `Parser.Invoked` will be set true. If `Parser.InvokeAction` is set, it will be executed with the state `Parser.Invoked`.
691
691
@@ -716,7 +716,7 @@ if e := p.Parse(nil); e != nil {
When argument is too long, you can set `ParserConfig.MaxHeaderLength` to a reasonable length.
722
722
@@ -748,11 +748,11 @@ options:
748
748
749
749
[example](examples/long-args/main.go)
750
750
751
-
#### 16. Inheriable argument [ >= 1.8.0]
751
+
#### 16. Inheriable argument [ >= 1.8 ]
752
752
753
753
When some argument represent the same thing among root parser and sub parsers, such as `debug`, `verbose` .... and you don't want to write dumplicate argument code for too many times, you have two recommended ways:
754
754
755
-
before *v1.8.0*, as all sub parsers are the same type as root parser, use a `for` loop with `Action` to do it.
755
+
before *v1.8*, as all sub parsers are the same type as root parser, use a `for` loop with `Action` to do it.
756
756
757
757
```go
758
758
parser:= argparse.NewParser("", "", nil)
@@ -773,7 +773,7 @@ if e := parser.Parse(nil); e != nil {
773
773
print(url)
774
774
```
775
775
776
-
The code might be less clean, and there comes `Inheritable` , which is an `argument option` . When argument with `&argparse.Option{Inheritable: true}`, sub parsers added __after__ the argument will be able to inherit this argument or override it.
776
+
The code might be less clean, and there comes `Inheritable` , which is an `argument option` . When argument with __`&argparse.Option{Inheritable: true}`__, sub parsers added __after__ the argument will be able to inherit this argument or override it.
777
777
778
778
```go
779
779
parser:= argparse.NewParser("", "", nil)
@@ -789,6 +789,32 @@ As a result, sub parser `local` will inhert `verbose` as a `Flag`, when pass us
789
789
790
790
[example](examples/inherit/main.go)
791
791
792
+
#### 17. Bind given parsers for an argument [ >= 1.9 ]
793
+
794
+
This is a very flexable way to create argument for multiple parsers. Create one argument using the main parser, and provide a series of parsers by setting the option when you create the argument:
795
+
796
+
__`Option{BindParsers: []*Parser{a, b, ...}}`__.
797
+
798
+
```go
799
+
parser:= argparse.NewParser("", "", nil)
800
+
a:= parser.AddCommand("a", "", nil)
801
+
b:= parser.AddCommand("b", "", nil)
802
+
c:= parser.AddCommand("c", "", nil)
803
+
804
+
ab:= parser.String("", "ab", &argparse.Option{
805
+
BindParsers: []*argparse.Parser{a, b},
806
+
})
807
+
bc:= parser.String("", "bc", &argparse.Option{
808
+
BindParsers: []*argparse.Parser{b, c},
809
+
})
810
+
```
811
+
812
+
As a result, subparser `a` & `b` will bind a `String` argument with entry `--ab` and referred to by var `ab` , subparser `b` & `c` will bind a `String` argument with entry `--bc` and referred to by var `bc`.
813
+
814
+
__Note__ that both arguments are detached from main `parser`, because you've given the `BindParsers`. If you still want to bind the argument to the main parser, append it to `BindParsers`, like `BindParsers: []*argparse.Parser{b, c, parser}`.
815
+
816
+
This is one way two share a single argument among different parsers. Be aware that creating argument like this still need to go through conflict check, if there's already a `--ab` exist in `a` or `b` parser, there will be a panic to notice the programer.
0 commit comments