Basic Concepts

Argument

Data passed sequentially in the program command line without name identifiers is called an Argument. The input of arguments is sequential.

Option

Additional input that controls the program logic and has a name identifier is called Options. Option names are prefixed with - or --. Options are unordered and can be placed in any position in the command line. Options may or may not have associated data. In other similar third-party functional components, options work similarly to Flags.

Additionally, following the traditional command line management practice, options can have shorthand aliases to simplify command line argument input. Shorthand aliases are often set as a single letter.

Option Positioning and = Sign

The gcmd component supports options being placed anywhere in the command line, meaning the following command line option inputs are essentially the same:

  1. gf build main.go -a amd64 -o linux -n app -yes
  2. gf -a amd64 -o linux build main.go -yes -n app
  3. gf -yes -n app build -o linux -a amd64 main.go

In these examples,

  • gf / build / main.go are arguments with indices 0, 1, 2 respectively; since arguments are ordered, changing the order in the command line does not change the order of these three.
  • a / o / n are options with data. Since the order is irrelevant, data is retrieved by option name, hence they can be placed freely.
  • yes is an option without data and can also be placed freely.

Options and their data can be connected with a space or an = sign, like:

  1. gf build main.go -a=amd64 -o=linux -n=app -yes

Default Parsing Rules

The gcmd module provides several package methods to obtain default command line parsing rules. By default, it will automatically recognize arguments and options.

Situations with the = Sign in the Command Line

  1. gf build main.go -a=amd64 -o=linux -n=app -yes

By default,

  • gf / build / main.go are arguments, with indices 0, 1, 2.
  • a / o / n / yes will be parsed as options, with yes being an option without data.

Not Using = Sign to Connect Option Parameters

  1. gf build main.go -a amd64 -o linux -n app -yes

By default,

  • gf / build / main.go are arguments, with indices 0, 1, 2.
  • a / o / n / yes will be parsed as options, with yes being an option without data.