integrii / flaggy

Idiomatic Go input parsing with subcommands, positional values, and flags at any position. No required project or package layout and no external dependencies.
The Unlicense
856 stars 30 forks source link

Support for trailing arguments #62

Closed SrslyJosh closed 4 years ago

SrslyJosh commented 4 years ago

Unless I've misunderstood the documentation, it doesn't seem like it's possible to have an arbitrary number of trailing arguments.

Let's say you want to write your own version of cat(1).

Based on the documentation, it seems like I should be able to use flaggy.TrailingArguments to get at the list of zero or more input files.

Unfortunately, if you actually try to use trailing arguments that aren't declared as positionals, flaggy throws an error:

$ go run main.go file1 file2 file3
<snip>
Unexpected argument: file1
exit status 2

Flaggy will accept trailing arguments if declared as positionals, but there does not seem to be a way to support an arbitrary number of them--each must be explicitly declared, so you can't actually write a cat command that takes an arbitrary number of file arguments.

integrii commented 4 years ago

Oh, simply try prepending your trailing arguments with --!

integrii commented 4 years ago

Like this:

./myProgram -- arg1 arg2 arg3 arg4...

craftyguy commented 2 years ago

requiring -- is awkward, since it is counterintuitive for users (i.e. what other commands out there require using -- to pass arguments directly to the exe being run?)

python's argparse allows this with nargs='*' (or nargs='+' to specify that at least 1 positional arg is required), it collects all remaining args into a list. So basically it's what flaggy does when -- is specified, but without having to include --