4.2. String[] args
¶
When a user launches a program using a shell command (i.e., when they
type the command into the prompt that appears in their terminal
emulator), any command-line arguments that the user includes after the
program name are passed into the program. How do they get in? In Java,
they are placed into an array of String
objects that can be
referred to using the main
method’s args
parameter.
public static void main(String[] args) {
...
} // main
This is a fairly common approach. Something very similar happens when
command-line arguments are supplied to programs written in C, C++, and
several other programming languages. The Python programming doesn’t
use main
as a method or function name like the other languages
mentioned so far do; however, it does place the command-line arguments
into a Python list that is accessible using Python’s sys.argv
variable.
int main(int argc, char* argv[]) {
...
} // main
if __name__ == '__main__':
args: list = sys.argv
...