Menu

Retrieval of Values

Chris DeGreef

The question is;

Once Argument has parsed the command-line how does my program get access to the values the user has entered?

Well, at least that is the question this page addresses. There is a much more elegant approach to getting the values but this is the most obvious way. See the documentation on injection of values for the elegant way.

First we define our parser. It will have a single String argument named --myArg. We must keep a reference to the Argument instance if we want to get the values out of it. So we assign it to a variable. And then we parse the command-line (commandLineArgs). Finally, we ask the Argument instance (argument) for the value associated with the --myArg argument. We have to cast the returned value since the Java compiler is unclear what type it actually will be.

~~~~~~
ICmdLine argument = CmdLine.create("--type string --key myArg");
argument.parse(commandLineArgs);
String myArgValue = (String) argument.arg("--myArg").getValue();

<pre name="code" class="html:nocontrols:nogutter">> MyProgram --myArg value1</pre>
<pre name="code" class="java:html:nocontrols">if (myArgValue.equals("value1")) ...</pre>

This is an example if the argument is defined with <code>--multiple</code>.  The only difference in accessing the value is providing an index to the getValue() method.

ICmdLine argument = CmdLine.create("--type string --key myArg -m 1 5");
argument.parse(commandLineArgs);
String myArgValue = (String) argument.arg("--myArg").getValue(0);

<pre name="code" class="html:nocontrols:nogutter">> MyProgram --myArg value1, value2</pre>
<pre name="code" class="java:html:nocontrols">if (myArgValue.equals("value1")) ...</pre>
You can find out how many values were entered with <code>size()</code>.

for (int x=0; x < myArgValue.size(); x++) {
String myArgValue = (String) argument.arg("--myArg").getValue(x);
...
}

It only gets a little more complex when you are using embedded Argument parsers.  Essentially, you ask the same questions to an embedded parser that you would ask to the top level parser.  This example will allow the user to enter groups of 1 to 5 strings with each group in brackets.  And in this example we have made everything <code>--positional</code>.

ICmdLine argument = CmdLine.create(
"--type begin --key group --positional -m 1 999" ,
"--type string --key myArg -m 1 5 --positional ",
"--type end --key group"
);
argument.parse(commandLineArgs);

<pre name="code" class="html:nocontrols:nogutter">> MyProgram (v1, v2) (v3, v4, v5, v6)</pre>
Since the embedded parser has the --multiple parameter as well we will need two loops to get all of the values.

for (int g=0; x < argument.arg("--group").size(); g++) {
ICmdLine embedded = (ICmdLine) argument.arg("--group").getValue(g);
for (int x=0; x < embedded.arg("--myArg").size(); x++) {
String myArgValue = (String) embedded.arg("--myArg").getValue(x);
}
}
~~~~~


Related

Wiki: Home
Wiki: Injection of Values

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.