When a program starts, arguments may be passed from the command line.
$ foo bar baz
In a traditional C/C++ program, these arguments are received as parameters to main()
. The Legato framework makes these available to component via function calls instead.
The arguments can be fetched by index using le_arg_GetArg()
. The first argument has index 0, the second argument has index 1, etc. In the above example, bar has index 0 and baz has index 1.
The number of available arguments is obtained using le_arg_NumArgs()
.
The name of the program is obtained using le_arg_GetProgramName()
.
The program name and all arguments are assumed to be Null-terminated UTF-8 strings. For more information about UTF-8 strings see UTF-8 String Handling API.
Options are arguments that start with a "-" or "--".
To search for a specific option, the following functions are provided:
Positional arguments are arguments that do not start with a "-" or "--"; except for "-" or "--" by itself (these are positional arguments).
For example, the following command line has four positional arguments ("foo", "bar", "-", and "--"). A flag option ("-x"), and two string options ("-f ./infile" and "--output=/tmp/output file") are intermixed with the positional arguments.
In this example, "foo" is the first positional argument, "-" is the second, "bar" is the third, and "--" is the fourth.
Positional arguments are retrieved using the Argument Scanner and le_arg_AddPositionalCallback().
If you're building a command-line application with a complex argument list, you may want to use the Legato framework's argument scanner feature. It supports many options commonly seen in command-line tools and performs a lot of the error checking and reporting for you.
For example, the commandLine
sample application implements a tool called fileInfo
that prints information about files or directories. It is flexible about the order of appearance of options on the command-line. For example, the following are equivalent:
# fileInfo -x -mc 20 permissions *
# fileInfo permissions --max-count=20 * -x
Note that
A program (typically inside a COMPONENT_INIT
) can call functions to register variables to be set or call-back functions to be called when certain arguments are passed to the program.
After registering the variables and call-back functions, le_arg_Scan() is called to parse the argument list.
The following functions can be called before le_arg_Scan() is called to register variables to be set or call-back functions to be called by le_arg_Scan():
There are essentially 3 forms of function:
le_arg_AddPositionalCallback() can be called multiple times. This constructs a list of call-back functions, where the first function in that list will be called for the first positional argument, the second function in the list will be called for the second positional argument, etc.
Normally, an error will be generated if there are not the same number of positional arguments as there are positional callbacks in the list. However, this behaviour can be changed:
le_utf8_ParseInt() can be used by a positional callback to convert the string value it receives into an integer value, if needed.
If a program wishes to try to recover from errors on the command-line or to generate its own special form of error message, it can use le_arg_SetErrorHandler() to register a callback function to be called to handle errors.
If no error handler is set, the default handler will print an error message to the standard error stream and terminate the process with an exit code of EXIT_FAILURE.
Error conditions that can be reported to the error handler are described in the documentation for le_arg_ErrorHandlerFunc_t.
If you are not using a main() function that is auto-generated by the Legato application framework's build tools (mksys
, mkapp
, or mkexe
), then you must call le_arg_SetArgs() to pass argc
and argv
to the argument parsing system before using any other le_arg
functions.
Copyright (C) Sierra Wireless Inc. Use of this work is subject to license.