forked from akamensky/argparse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Designed to begin addressing issue akamensky#29 HelpFunc added as field for Parsers and Commands Parser HelpFunc defaults to Usage unless overwritten Command HelpFunc defaults to first non nil parent HelpFunc Design choice to enable backwards compatibility required exposing Command and arg fields with Getter functions and an interface. Does not resolve: exiting program on help invocation overwrritting/disabling -h | --help argument strings enabling other argument strings to invoke help
- Loading branch information
1 parent
e643750
commit d52dd7f
Showing
5 changed files
with
211 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
argparse "../.." | ||
) | ||
|
||
func main() { | ||
// Create new parser object | ||
parser := argparse.NewParser("help", "Demonstrates overriding the default help output") | ||
// Replace parser.Usage as the help message | ||
parser.HelpFunc = func(c argparse.Command, msg interface{}) string { | ||
var help string | ||
help += fmt.Sprintf("Name: %s, Description: %s\n", c.GetName(), c.GetDescription()) | ||
for _, arg := range c.GetArgs() { | ||
if arg.GetOpts() != nil { | ||
help += fmt.Sprintf("Sname: %s, Lname: %s, Help: %s\n", arg.GetSname(), arg.GetLname(), arg.GetOpts().Help) | ||
} else { | ||
help += fmt.Sprintf("Sname: %s, Lname: %s\n", arg.GetSname(), arg.GetLname()) | ||
} | ||
} | ||
return help | ||
} | ||
|
||
// Create command | ||
command := parser.NewCommand("sub", "Demonstrates sub command help") | ||
// Create string flag | ||
command.String("s", "string", &argparse.Options{Required: true, Help: "String argument example"}) | ||
// Create string flag | ||
command.Int("i", "int", &argparse.Options{Required: true, Help: "Integer argument example"}) | ||
// Without declaring command help function, defaults to first non nil parent help function | ||
fmt.Print(command.Help(nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
argparse "../.." | ||
) | ||
|
||
func main() { | ||
// Create new parser object | ||
parser := argparse.NewParser("help", "Demonstrates overriding the default help output") | ||
// Create string flag | ||
parser.String("s", "string", &argparse.Options{Required: true, Help: "String argument example"}) | ||
// Create string flag | ||
parser.Int("i", "int", &argparse.Options{Required: true, Help: "Integer argument example"}) | ||
// Replace parser.Usage as the help message | ||
parser.HelpFunc = func(c argparse.Command, msg interface{}) string { | ||
var help string | ||
help += fmt.Sprintf("Name: %s, Description: %s\n", c.GetName(), c.GetDescription()) | ||
for _, arg := range c.GetArgs() { | ||
if arg.GetOpts() != nil { | ||
help += fmt.Sprintf("Sname: %s, Lname: %s, Help: %s\n", arg.GetSname(), arg.GetLname(), arg.GetOpts().Help) | ||
} else { | ||
help += fmt.Sprintf("Sname: %s, Lname: %s\n", arg.GetSname(), arg.GetLname()) | ||
} | ||
} | ||
return help | ||
} | ||
// Use the help function | ||
fmt.Print(parser.Help(nil)) | ||
} |