Building CLI applications
Our focus from the initial script example in Chapter 1, Getting Started, was on using CLI scripting to learn Python basics. CLI applications have a number of common features:
- They often read from the standard input file, write to a standard output file, and produce logs or errors in the standard error file. The OS assures us that these files are always available. Python provides them as
sys.stdin
,sys.stdout
, andsys.stderr
. Furthermore, functions such asinput()
andprint()
use these files by default. - They often use environment variables for configuration. These values are available through
os.environ
. - They may also rely on shell features, like expanding
~
into a user's home directory, something done byos.path.expanduser()
. - They often parse command-line arguments. While the variable
sys.argv
has the argument strings, these are awkward to work with directly. We'll use theargparse
module to define the argument patterns, parse the strings, and create an object...