L1VM - cli arguments

Here I will show how to get the cli arguments in Brackets.

shell_args (args)

Here the number of shell arguments is stored in the variable “args”. Zero means there is no argument.

get_shell_arg (i, shell_arg)

Here the argument “i” will be stored in the string variable “shell_arg”. If there are two arguments then you would use “0” and “1” as values for “i”!

Here is an example cli call:

$ l1vm prog/shell-args -args foo bar foobar

You have to add the arguments behind the “-args” flag. Without this flag it won’t work!

Here is a full example:

// show shell arguments
#include <intr.l1h>
(main func)
	(set int64 1 zero 0)
	(set int64 1 one 1)
	(set int64 1 args 0)
	(set string 255 shell_arg "")
	(set int64 1 i 0)
	(set int64 1 f 0)
	(set string s argstr " shell arguments: ")
	// get number of shell arguments:
	shell_args (args)
	print_i (args)
	print_s (argstr)
	print_n
	print_n
	(((args zero ==) f =) f if)
	// no arguments, exit!
		(255 0 0 0 intr0)
	(endif)
	(zero i =)
	(:loop)
	// get shell argument in variable "shell_arg"
	get_shell_arg (i, shell_arg)
	print_s (shell_arg)
	print_n
	((i one +) i =)
	(((i args <) f =) f if)
		(:loop jmp)
	(endif)
	exit (zero)
(funcend)
$ l1vm prog/shell-args -q -args foo bar foobar
3 shell arguments: 

foo
bar
foobar

11: preprocessor