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

Can you require that a specific flag is used? #48

Closed atc0005 closed 5 years ago

atc0005 commented 5 years ago

I saw this in the README:

 Positional Variables:
    testPositionalA - (Required) Test positional A does some things with a positional value. (default: defaultValue)
    testPositionalB - Test positional B does some less than serious things with a positional value.

and when reviewing the GoDoc page for this package I saw that positional arguments can optionally be required, but didn't spot that feature when configuring flags.

Is there an easy way to determine when a flag has been provided? Would you perhaps define a "default" instance of a configuration type to compare against the configuration type that is modified by flaggy.Parse()?

integrii commented 5 years ago

For flags, you can easily just check the value after calling flaggy.Parse(). If the value is set to something other than the default, then the caller supplied it. If it was the default value (set by you or the language), then it was not used.

As for subcommands, you can check the Used property:

if subcommandA.Used { print("subcommand A used") }

Positional commands have a requirement parameter because leaving out positionals can cause the variables being input to shift, which is much harder to validate against.

atc0005 commented 5 years ago

For flags, you can easily just check the value after calling flaggy.Parse(). If the value is set to something other than the default, then the caller supplied it. If it was the default value (set by you or the language), then it was not used.

Thanks for confirming I was on the right path. I've included a response to this issue I was about to submit when I noticed your reply. The package might be overkill for the purposes of determining whether a flag was passed, but seems to be useful for my learning/prototype work.

As for subcommands, you can check the Used property:

if subcommandA.Used { print("subcommand A used") }

Positional commands have a requirement parameter because leaving out positionals can cause the variables being input to shift, which is much harder to validate against.

Ah, OK. That makes sense. Thanks for taking the time to explain all of this.


My original response:

Follow-up to my earlier post: I came across this package which allows you to diff structures for changes. This could be used against a "stock" copy of the config struct and a copy of the config struct (potentially) updated by flaggy.Parse().