Command line input example

This example reads input from command line.

Compilation

Easiest way to get this example running is to go to examples directory in package root, and run:

$ ./build.sh cmdline_input

build.sh script compiles Ano script to C, copies source files in place and pops up instructions what to do next. Follow them. Check also examples/README for more info.

Preview

; ------------------------------------------------------------------------------
; cmdline_input.ano
;
; To compile:
;
; $ ./build/ano ./examples/cmdline_input/cmdline_input.ano > \
;     engine/dsl_ano.h
; ------------------------------------------------------------------------------
;
@ANO_SCRIPT_NAME		cmdline_input
@ANO_SCRIPT_VERSION		0.0.1
@ANO_SCRIPT_DESCRIPTION	Example how to read input from command line
;
@ANO_FLAGS_USE_PROTOS		[ ]
@ANO_FLAGS_VAR_NAME_SUBS	[ ]
@ANO_FLAGS_VAR_WARN_UNUSED	[ ]
;
; Copyright © 2016-2026, Jani Salonen <salojan@goto10.co>
; All rights reserved.
;
; This example reads input from command line.
;
; Possible ways to get the input is listed below. General syntax for command
; line is:
;
;  $ application --engine-param1 --engine-param2 \
;    EXISTING_FILE1 param1_for_file1 \
;    EXISTING_FILE2 param1_for_file2 param2_for_file2 …
;  $ application -- param1 param2 …
;
; First example reads input and dumps it out:
;
;  $ ./cmdline_input
;  XXX <enter>
;    rc is string (UTF-8) "XXX"
;    There are 0 options in command line.
;    There are 0 files provided in command line.
;
; Read input through pipe:
;
;  $ echo XXX | ./cmdline_input
;    rc is string (UTF-8) "XXX"
;    There are 0 options in command line.
;    There are 0 files provided in command line.
;
; Read input, get existing file name and three parameters for it:
;
;  $ echo XXX | ./cmdline_input cmdline_input 1 2 3
;    rc is string (UTF-8) "XXX"
;    There are 0 options in command line.
;    There are 1 files provided in command line.
;     file 0 is cmdline_input.
;     there are 3 parameters for cmdline_input.
;      parameter 0 is 1.
;      parameter 1 is 2.
;      parameter 2 is 3.
;
; Read input, use engine parameter and four plain parameters for this script:
;
;  $ echo XXX | ./cmdline_input -du -- nonexistent 1 2 3
;    rc is string (UTF-8) "XXX"
;    There are 0 options in command line.
;    There are 1 files provided in command line.
;     file 0 is cmdline_input.
;     there are 4 parameters for cmdline_input.
;      parameter 0 is nonexistent.
;      parameter 1 is 1.
;      parameter 2 is 2.
;      parameter 3 is 3.
;
; Application parameters can be read too, if they are defined in
; work/cmdline_app.h:
;
;  $ ./cmdline_input --my_parameter
;

main [exit: 0] {
	; Read from stdin and dump it out.
	;
	input_read (mode: INPUT_TERM_CRLF)

	dump rc

	; Get amount of app specific options in command line.
	;
	_args = cmdline_get_args

	print "There are " . _args . " options in command line.\n"

	for (mov _i (0); _i < _args; inc _i) {
		; Get option short name by index.
		;
		_short = cmdline_get_arg (arg_index: _i)

		print " option " . _i . " short name is " . _short . ".\n"

		; Get option long name by index.
		;
		_long = cmdline_get_arg_long (arg_index: _i)

		print " option " . _i . " long name is " . _long . ".\n"

		; Get option value by index skipping secure options.
		;
		_value = cmdline_get_arg_by_index (arg_index: _i)

		print " option value is " . _value . ".\n"

		; Get option value by arg short or long name skipping
		; secure options.
		;
		_value = cmdline_get_arg_by_name (arg_name: _short)

		print "\n"
	}

	; Get amount of files provided in command line, files are existing
	; files not attached to any keyword.
	;
	_files = cmdline_get_files

	print "There are " . _files . " files provided in command line.\n"

	for (mov _i (0); _i < _files; inc _i) {
		; Get file name by index.
		;
		_file = cmdline_get_file (file_index: _i)

		print " file " . _i . " is " . _file . ".\n"

		; Get amount of params for file by index.
		;
		_args = cmdline_get_file_args (file_index: _i)

		print " there are " . _args . " parameters for " . _file . ".\n"

		for (mov _j (0); _j < _args; inc _j) {
			; Get file param by file index and parameter index.
			;
			_arg = cmdline_get_file_arg (\
				file_index: _i, \
				arg_index: _j)

			print "  parameter " . _j . " is " . _arg . ".\n"
		}

		print "\n"
	}
}
; ------------------------------------------------------------------------------
; cmdline_input.w2c
;
; To compile:
;
; $ ./build/widget ./examples/cmdline_input/cmdline_input.w2c > \
;     engine/widget_defs.h
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; cmdline_input.m2c
;
; To compile:
;
; $ ./build/menu ./examples/cmdline_input/cmdline_input.m2c > \
;     engine/menu_defs.h
; ------------------------------------------------------------------------------

Copyright © 2026, Jani Salonen <salojan at goto10 piste co>. Piste is finnish word and means dot. All rights reserved.