Command-line Arguent Examples
Python:
Let's remind ourselves by running this.
from sys import argv
print(argv)
print(type(argv))
The object argv
is a list of strings. The
first element in the list is the name of the program.
unix> python commando.py ['commando.py'] <class 'list'>
Now let's have the user enter some command-line arguments.
unix> python commando.py cow pig horse elephant ['commando.py', 'cow', 'pig', 'horse', 'elephant'] <class 'list'>
The rest of the items are strings typed in by the user of the program at the command line.
Java:
We do an import of the class Arrays
here
becuse Java arrays do not know how to print themselves
nicely
The call Arrays.toString
will make this
happen.
import java.util.Arrays;
public class Commando
{
public static void main(String[] args)
{
System.out.println(Arrays.toString(args));
}
}
unix> javac Commando unix> java Commando [] unix> java Commando pizza hoagie hamburger [pizza, hoagie, hamburger]
Note that the array args
does not
include the name of the program.
These guys can reach across the top -|G -|R -|E -|A instance land -|S static land state variables -|E static data (ORIGIN) non-static methods -| static methods -------------------------------------------------------