Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples of documenting positional arguments #223

Open
wolever opened this issue Feb 19, 2017 · 2 comments
Open

Examples of documenting positional arguments #223

wolever opened this issue Feb 19, 2017 · 2 comments

Comments

@wolever
Copy link

wolever commented Feb 19, 2017

It would be nice to have some examples showing how positional arguments (ie, the returned args) array can be documented in the help output.

Right now it's not clear how to describe them, show default values, examples, etc.

@ekle
Copy link

ekle commented Jan 5, 2020

Is just the documentation missing or is this not possible at all?

@gsauthof
Copy link

gsauthof commented Aug 30, 2020

I also couldn't find any documentation on how to parse positional arguments although there is a comment that mentions documentation.

After reading that description and looking at some test cases in this repo I managed to build this minimal example:

package main

import (
    "log"
    "os"
    "github.com/jessevdk/go-flags"
)

type args struct {
    Verbose bool `short:"v" long:"verbose" description:"verbose output"`
    Positional struct {
	Dir string `positional-arg-name:"DIRECTORY"`
    } `positional-args:"true" required:"true"`
}

func main() {
    var args args
    _, err := flags.Parse(&args)

    if e, ok := err.(*flags.Error); ok {
        if e.Type == flags.ErrHelp {
            os.Exit(0)
        } else {
            os.Exit(1)
        }
    }

    if err != nil {
	log.Fatal(err)
    }

    log.Println(args)
}

Example output:

$ ./getargs -h  
Usage:
  getargs [OPTIONS] DIRECTORY

Application Options:
  -v, --verbose    verbose output

Help Options:
  -h, --help       Show this help message

$ echo $?
0
$ ./getargs   
the required argument `DIRECTORY` was not provided
$ echo $?
1
$ ./getargs path/to
2020/08/30 20:57:34 {false {path/to}}
$ ./getargs path/to trailing
2020/08/30 21:00:20 {false {path/to}}
$ echo 'expected error due to extra positional argument'
expected error due to extra positional argument

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants