Skip to content

Commit

Permalink
Merge pull request #3 from vsachs/get_argument_result
Browse files Browse the repository at this point in the history
Add examples & documentation
  • Loading branch information
vsachs committed May 24, 2021
2 parents 613c5e1 + 1436261 commit ca45f72
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -137,6 +137,13 @@ You can implement sub-commands in your CLI using `parser.NewCommand()` or go eve
Since parser inherits from command, every command supports exactly same options as parser itself,
thus allowing to add arguments specific to that command or more global arguments added on parser itself!

You can also dynamically retrieve argument values:
```
var myInteger *int = parser.Int("i", "integer", ...)
parser.Parse()
fmt.Printf("%d", *parser.GetArgs()[0].GetResult().(*int))
```

#### Basic Option Structure

The `Option` structure is declared at `argparse.go`:
Expand Down
7 changes: 7 additions & 0 deletions examples/commands/commands.go
Expand Up @@ -16,6 +16,7 @@ func main() {

// Add top level commands `stop`
stopCmd := parser.NewCommand("stop", "Will stop a process")
stopCmd.Int("-t", "--time", nil)

// Parse command line arguments and in case of any error print error and help information
err := parser.Parse(os.Args)
Expand All @@ -30,6 +31,12 @@ func main() {
fmt.Println("Started process")
} else if stopCmd.Happened() { // Check if `stop` command was given
// Stopping a process
for _, arg := range stopCmd.GetArgs() {
switch val := arg.GetResult().(type) {
case int:
fmt.Printf("Arg: %s = %d\n", arg.GetLname(), val)
}
}
fmt.Println("Stopped process")
} else {
// In fact we will never hit this one
Expand Down

0 comments on commit ca45f72

Please sign in to comment.