-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from carbocation/master
Add interpolation choice flag
- Loading branch information
Showing
2 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
) | ||
|
||
// Via https://stackoverflow.com/a/74146375/199475 | ||
|
||
// ParseFlags parses the command line args, allowing flags to be | ||
// specified after positional args. | ||
func ParseFlags() error { | ||
return ParseFlagSet(flag.CommandLine, os.Args[1:]) | ||
} | ||
|
||
// ParseFlagSet works like flagset.Parse(), except positional arguments are not | ||
// required to come after flag arguments. | ||
func ParseFlagSet(flagset *flag.FlagSet, args []string) error { | ||
var positionalArgs []string | ||
for { | ||
if err := flagset.Parse(args); err != nil { | ||
return err | ||
} | ||
// Consume all the flags that were parsed as flags. | ||
args = args[len(args)-flagset.NArg():] | ||
if len(args) == 0 { | ||
break | ||
} | ||
// There's at least one flag remaining and it must be a positional arg since | ||
// we consumed all args that were parsed as flags. Consume just the first | ||
// one, and retry parsing, since subsequent args may be flags. | ||
positionalArgs = append(positionalArgs, args[0]) | ||
args = args[1:] | ||
} | ||
// Parse just the positional args so that flagset.Args()/flagset.NArgs() | ||
// return the expected value. | ||
// Note: This should never return an error. | ||
return flagset.Parse(positionalArgs) | ||
} |