Writing command line tools
To write a command line tool in Python, open a plain text file named
hello.py
and enter the following contents
#!/usr/bin/env python3
print('Hello, World!')
Save the file and make it executable with following shell command
% chmod u+x hello.py
Now, you should be able to execute your script from the command line
% ./hello.py
Hello, World!
Command line arguments
Let's assume that you want to write a script to download data from your local
XNAT
installation. You'll need to accept the command line arguments --hostname
,
--session
, and --output-dir
% ./download.py --hostname xnat.example.com \
--session LABEL \
--output-dir ./output
Python includes a traditional C-style parser
getopt
,
but there's a more convenient way to define and parse command line
arguments using
argparse
.
argparse
defining command line arguments
To define command line arguments, import the argparse
module and create an
instance of
argparse.ArgumentParser
import argparse
parser = argparse.ArgumentParser()
Now you can add your arguments to the parser
object by calling
parser.add_argument
parser = argparse.ArgumentParser()
parser.add_argument('--hostname')
parser.add_argument('--project')
parser.add_argument('--label')
parser.add_argument('--output-dir')
parsing the command line
To parse the command line arguments entered by the user, simply call
parser.parse_args
args = parser.parse_args()
accessing the command line arguments
Each command line argument is automatically converted into a property on the
object returned by parse_args()
. The name of the property is similar (but
not identical) to the name of the argument that was declared. There are two
translation rules to remember
- Remove any leading dashes e.g.,
-
or--
, from the argument name - Replace any embedded dash
-
with an underscore_
print(f'host is {args.hostname}')
print(f'project {args.project}')
print(f'label is {args.label}')
print(f'out_dir is {args.output_dir}')