https://github.com/coalaura/arguments
Library for parsing command line arguments and make them easy to use in various types.
https://github.com/coalaura/arguments
Last synced: 3 months ago
JSON representation
Library for parsing command line arguments and make them easy to use in various types.
- Host: GitHub
- URL: https://github.com/coalaura/arguments
- Owner: coalaura
- Created: 2024-09-08T21:02:18.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2025-01-22T15:41:27.000Z (5 months ago)
- Last Synced: 2025-02-01T22:46:52.622Z (4 months ago)
- Language: Go
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Sure! Below is a README for your package.
---
# Arguments
`arguments` is a Go package that simplifies the process of parsing command-line arguments. It supports both short (`-a`) and long (`--argument`) flags, and can automatically generate help messages.
## Installation
To install the package, run:
```sh
go get github.com/coalaura/arguments
```## Usage
Here's a quick example of how to use the `arguments` package in your Go project.
### Registering Arguments
First, you need to register the arguments you want to parse. You can register both short and long arguments and provide default values for them.
```go
package mainimport (
"github.com/coalaura/arguments"
)func main() {
var (
full string
short string
long string
num uint32
boolean bool
)arguments.Register("string", 's', &str).WithHelp("A string argument with both a short and long name")
arguments.Register("", 'a', &short).WithHelp("A short argument with no long name")
arguments.Register("long", 0, &long).WithHelp("A long argument with no short name")
arguments.Register("number", 'n', &num).WithHelp("A number argument")
arguments.Register("boolean", 'b', &boolean).WithHelp("A boolean argument")arguments.MustParse()
}
```### Parsing Arguments
After registering the arguments, you need to call `MustParse()` to parse the command-line arguments. You can also call `Parse()` if you want to handle errors yourself.
```go
arguments.MustParse()
```### Help
You can display a help message that lists all the registered arguments and their descriptions by calling `ShowHelp()` or `ShowHelpAndExit()`.
```go
arguments.ShowHelp(true)
```Alternatively you can call `RegisterHelp()` to register a help argument, which will display the help message and exit the program if set.
```go
arguments.RegisterHelp(true)
```## Example
Below is a complete example showing how to use the package:
```go
package mainimport (
"github.com/coalaura/arguments"
"fmt"
)func main() {
var (
input string
output string
number uint32
boolean bool
)arguments.Register("input", 'i', &input).WithHelp("The input file")
arguments.Register("output", 0, &output).WithHelp("The output file")
arguments.Register("", 'n', &number).WithHelp("Number of things")
arguments.Register("bool", 'b', &boolean).WithHelp("A boolean flag")arguments.MustParse()
fmt.Printf("Input: %s\n", input)
fmt.Printf("Output: %s\n", output)
fmt.Printf("Number: %d\n", number)
fmt.Printf("Boolean: %v\n", boolean)arguments.ShowHelp(true)
}
```## Tests
To run the tests for this package, use the `go test` command:
```sh
go test
```