Skip to main content

args Command-Line Interaction

args is Alia's standard library command-line interaction module, providing command-line argument definition, parsing, callback triggering, help generation, and more.

Importing the Module​

import 'args' as args ~ import as a variable
import 'args' ~ import into scope

Creating a Parser​

create​

Creates an argument parser instance and returns an ArgsParser object.

ParameterTypeRequiredDescription
Argument after namedStringNoApplication name, used in help text
Argument after describedStringNoApplication description, used in help text
const parser = args.create named 'myapp' described 'My application'

Parser Methods​

addOption​

Adds a command-line option that takes a value (e.g. --name value).

ParameterTypeRequiredDescription
First positional argumentStringYesFlag definition, e.g. '-n --name', with the short/long flags separated by spaces
typeStringNoValue type: 'String' (default), 'Number', 'Boolean'
defaultValueNoDefault value
requiredBooleanNoWhether it is required, defaults to false
descriptionStringNoOption description, used in help text
actionAliasNoCallback, triggered when the option is parsed; receives the parsed value
parser.addOption '-n --name' type 'String' required true description 'Your name' action (alias -> <String value> {
print 'Hello, ' .. value
})

parser.addOption '-c --count' type 'Number' default 1 description 'Repeat count'

addFlag​

Adds a boolean flag (no value needed; its presence means true).

ParameterTypeRequiredDescription
First positional argumentStringYesFlag definition, e.g. '-v --verbose'
descriptionStringNoFlag description
actionAliasNoCallback, triggered when the flag is parsed; takes no arguments
parser.addFlag '-v --verbose' description 'Enable verbose mode' action (alias -> {
print 'Verbose mode enabled'
})

addArgument​

Adds a positional argument definition.

ParameterTypeRequiredDescription
First positional argumentStringYesArgument name
descriptionStringNoArgument description
requiredBooleanNoWhether it is required, defaults to false
defaultValueNoDefault value
actionAliasNoCallback, triggered when the argument is parsed; receives the parsed value
parser.addArgument 'input' description 'Input file path' required true action (alias -> <String value> {
print 'Reading from ' .. value
})
parser.addArgument 'output' description 'Output file path'

parse​

Performs argument parsing and returns the parsed result object. During parsing, the action callback of each argument is triggered in match order.

ParameterTypeRequiredDescription
Arguments to parseStringNoCan be passed one by one; if omitted, process.argv is used

The returned object has the following structure:

PropertyTypeDescription
optionsObjectOption key-value pairs
argumentsObjectPositional argument key-value pairs
const parsed = call parser.parse
print parsed.options.name
print parsed.arguments.input

get​

Gets the value with the given name from the parse result. Returns Null if nothing has been parsed or the name does not exist.

const name = parser.get 'name'
const verbose = parser.get 'verbose'

help​

Generates and returns a formatted help text string.

print call parser.help

Parsing Rules​

Input formDescription
--long valueLong option with a value (space-separated)
--long=valueLong option with a value (equals-separated)
--flagBoolean flag; its presence means true
-vShort flag
-vfCombined short flags, equivalent to -v -f
-n valueShort option with a value
Not starting with -Matched as a positional argument in definition order
--Everything after it is treated as positional arguments

Example Code​

import 'args' as args

const parser = args.create named 'greet' described 'A greeting CLI tool'

parser.addFlag '-v --verbose' description 'Enable verbose output' action (alias -> {
print '[verbose] Starting...'
})

parser.addOption '-n --name' type 'String' required true description 'Name to greet'

parser.addOption '-c --count' type 'Number' default 1 description 'Greeting count'

parser.addArgument 'greeting' description 'Greeting word' default 'Hello'

const parsed = call parser.parse

const name = parser.get 'name' as String
const count = parser.get 'count' as Number
const greeting = parser.get 'greeting' as String

let i = 0
while i < count {
print greeting .. ', ' .. name .. '!'
i++
}

Command-line invocation:

alia greet.alia -v --name Alice -c 3 Hi
~ [verbose] Starting...
~ Hi, Alice!
~ Hi, Alice!
~ Hi, Alice!

Next Steps​