Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/GoNZooo/odin-cli
A library for parsing command line arguments according to type specs in Odin
https://github.com/GoNZooo/odin-cli
Last synced: 18 days ago
JSON representation
A library for parsing command line arguments according to type specs in Odin
- Host: GitHub
- URL: https://github.com/GoNZooo/odin-cli
- Owner: GoNZooo
- Created: 2023-08-15T15:20:28.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-09T15:41:21.000Z (4 months ago)
- Last Synced: 2024-08-09T17:30:04.471Z (4 months ago)
- Language: Odin
- Size: 59.6 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-odin - CLI Argument Parser
- awesome-odin - CLI Argument Parser
README
# odin-cli
A library for using types to specify how command line arguments should be parsed.
## Usage
```odin
// This is a union specifying a command, i.e. `unthread analyze-lock-file ...`
Command :: union {
AnalyzeLockFile,
}// The arguments that `analyze-lock-file` expects are specified in the payload
// and should be a struct.
AnalyzeLockFile :: struct {
// We can optionally specify short and long names, as well as say that an
// argument is required. If we don't specify anything only the long name
// will be generated and the argument will be optional (zero-initialized).
filename: string `cli:"f,filename/required"`,
}main :: proc() {
arguments := os.args
if len(arguments) < 2 {
fmt.println("Usage: unthread arguments...")
os.exit(1)
}// When we specify here that `Command` is the type that we want to parse
// `cli` will look at the possible union variants it has and try to parse
// them as kebab-case as well as their payloads.
command, remaining_arguments, cli_error := cli.parse_arguments_as_type(arguments[1:], Command)
if cli_error != nil {
fmt.println("Failed to parse arguments: ", cli_error)
os.exit(1)
}// Down here we know that we've successfully parsed an argument, so we can
// run the corresponding logic.
switch c in command {
case AnalyzeLockFile:
run_analyze_lock_file(c)
}
}
```