required-args

command
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2022 License: MIT Imports: 3 Imported by: 0

README

Required vs Optional Arguments

The purpose of this example is to demonstrate the difference between required and optional arguments using string arguments.

Setup

The first step for any project is to build a new parser:

parser := argparse.NewParser("Required-args", "Example of required vs optional arguments")

For this demonstration, two shorthand arguments are used:

// Required shorthand argument
foo := parser.String("f", "foo", &argparse.Options{Required: true, Help: "foo is a required string option"})

// Optional shorthand argument
bar := parser.String("b", "bar", &argparse.Options{Required:false, Help: "bar is not a required option"})

foo is a required string argument, and bar is an optional string argument.

Using required-arguments.go without foo demonstrates this:

$ go run required-args.go
[-f|--foo] is required
usage: Required-args [-h|--help] -f|--foo "<value>" [-b|--bar "<value>"]

                     Example of required vs optional arguments

Arguments:

   -h   --help    Print help information
   -f   --foo     foo is a required string option
   -b   --bar     bar is not a required option


Output when foo is provided:

$ go run required-args.go --foo foo
Provided arguments:
foo (required): foo

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL