Skip to content

Library for parsing command line arguments and make them easy to use in various types.

Notifications You must be signed in to change notification settings

coalaura/arguments

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

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.

package main

import (
    "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.

arguments.MustParse()

Help

You can display a help message that lists all the registered arguments and their descriptions by calling ShowHelp() or ShowHelpAndExit().

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.

arguments.RegisterHelp(true)

Example

Below is a complete example showing how to use the package:

package main

import (
    "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:

go test

About

Library for parsing command line arguments and make them easy to use in various types.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages