Closed atc0005 closed 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.
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()
.
I saw this in the README:
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()
?