Background: what is NGS?
NGS, the Next Generation Shell is a (work in progress) shell and a programming language built ground up for systems engineering tasks. You can think of it as bash that’s designed today: sane syntax, data structures, functional programming, extensibility, cloud in mind, declarative primitives.
What’s the problem with constructing command line arguments?
The problem affects only more “advanced” cases of constructing command line arguments when some arguments might or might not be present. Let’s consider this example:
# Made-up syntax, resembling NGS
args = []
if 'Subnets' in props {
args += '--subnets'
args += props['Subnets']
}
if ... {
args += ...
}
if ... {
args += ...
}
...
aws elb create-load-balancer ... $args
Wouldn’t it be cleaner to get rid of all the ifs? … and what happens if props['Subnets']
is an empty array?
How Argv facility in NGS solves the problem?
Argv
is a result of factoring out the common code bits involved in constructing command line arguments. The if
s above were also factored out. They are now in Argv
.
Let’s look at usage example (real NGS code, from AWS library)
argv = Argv({
'--load-balancer-name': rd.anchor.name
'--listeners': props.ListenerDescriptions.encode_json()
'--subnets': rd.opt_prop('Subnets', props).map(only(ResDef, ids))
})
rd.run('create ELB', %(aws elb create-load-balancer $*argv))
The important points here are:
Argv
is a function with a single parameter which must be of typeHash
(also called “dictionary” in some languages)- The keys of the Hash are switches’ names (
--load-balancer-name
,--listeners
,--subnets
) - The values of the Hash are values for the switches
The “if” that decides whether a switch is present in the resulting argv
is inside Argv
implementation and your code is clean of it. The values of the Hash
are considered when Argv
decides whether a switch should be present. null
, empty array and instances of type EmptyBox
are considered by Argv
as missing values and it discards the switch. For convenience, instances of type FullBox
are unboxed when constructing the result of Argv
.
The Argv facility is yet another point among others that shows why NGS and systems engineering tasks are best fit.
Have a nice weekend!