Skip to content

Instantly share code, notes, and snippets.

@rpdelaney
Last active October 7, 2022 15:11
Show Gist options
  • Save rpdelaney/15725901c980ccc6c93d5615b869174f to your computer and use it in GitHub Desktop.
Save rpdelaney/15725901c980ccc6c93d5615b869174f to your computer and use it in GitHub Desktop.
python cli tools in the standard library

You get a lot of command-line tools just for having python 3 installed.

$ # despite the name, compares files found in two _directories_ (non-recursive)
$ python3 -m filecmp dumbpw/ tests/
diff dumbpw/ tests/
Only in dumbpw/ : ['candidate.py', 'charspace.py', 'cli.py', 'pwgen.py']
Only in tests/ : ['test_charspace', 'test_pwgen']
Differing files : ['__init__.py']
$ # print a calendar
$ python3 -m calendar
[...]
$ # json operations
$ python3 -m json.tool --help
usage: python -m json.tool [-h] [--sort-keys] [--no-ensure-ascii] [--json-lines] [--indent INDENT | --tab | --no-indent | --compact] [infile] [outfile]

A simple command line interface for json module to validate and pretty-print JSON objects.

positional arguments:
  infile             a JSON file to be validated or pretty-printed
  outfile            write the output of infile to outfile

options:
  -h, --help         show this help message and exit
  --sort-keys        sort the output of dictionaries alphabetically by key
  --no-ensure-ascii  disable escaping of non-ASCII characters
  --json-lines       parse input using the JSON Lines format. Use with --no-indent or --compact to produce valid JSON Lines output.
  --indent INDENT    separate items with newlines and use this number of spaces for indentation
  --tab              separate items with newlines and use tabs for indentation
  --no-indent        separate items with spaces rather than newlines
  --compact          suppress all whitespace separation (most compact)

$ # ━━━ clients ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
$ # open a web browser
$ python3 -m webbrowser
Usage: /Users/ryan/.asdf/installs/python/3.10.0/lib/python3.10/webbrowser.py [-n | -t] url
    -n: open new window
    -t: open new tab
$ # telnet client
$ python3 -m telnetlib freechess.org 5000
[...]
$ # spartan ftp client
$ python3 -m ftplib -h
Test program.
    Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...

    -d dir
    -l list
    -p password

$ # ━━━ debugging ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
$ # list the modules imported by the provided script argument, and their location (!)
$ python3 -m modulefinder <script.py>
[...]
$ # show info about site-specific config
$ python3 -m site
sys.path = [
    '/Users/ryan/src/me',
    '/Users/ryan/.asdf/installs/python/3.10.0/lib/python310.zip',
    '/Users/ryan/.asdf/installs/python/3.10.0/lib/python3.10',
    '/Users/ryan/.asdf/installs/python/3.10.0/lib/python3.10/lib-dynload',
    '/Users/ryan/.local/lib/python3.10/site-packages',
    '/Users/ryan/.asdf/installs/python/3.10.0/lib/python3.10/site-packages',
]
USER_BASE: '/Users/ryan/.local' (exists)
USER_SITE: '/Users/ryan/.local/lib/python3.10/site-packages' (exists)
ENABLE_USER_SITE: True
$ # Python's configuration information
$ python3 -m sysconfig
[...]
$ # dump a bunch of information about the local shell environment to html via stdout
python3 -m cgi
[...]
$ # print the local platform
$ python3 -m platform
macOS-12.1-arm64-arm-64bit

$ # ━━━ archivers ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
$ # tar
$ python3 -m tarfile -h
usage: tarfile.py [-h] [-v] (-l <tarfile> | -e <tarfile> [<output_dir> ...] | -c <name> [<file> ...] | -t <tarfile>)

A simple command-line interface for tarfile module.

options:
  -h, --help            show this help message and exit
  -v, --verbose         Verbose output
  -l <tarfile>, --list <tarfile>
                        Show listing of a tarfile
  -e <tarfile> [<output_dir> ...], --extract <tarfile> [<output_dir> ...]
                        Extract tarfile into target dir
  -c <name> [<file> ...], --create <name> [<file> ...]
                        Create tarfile from sources
  -t <tarfile>, --test <tarfile>
                        Test if a tarfile is valid
$ # GNU zip
$ python3 -m gzip < file.txt > file.txt.gz
$ python3 -m gzip -h
usage: gzip.py [-h] [--fast | --best | -d] [file ...]

A simple command line interface for the gzip module: act like gzip, but do not delete the input file.

positional arguments:
  file

options:
  -h, --help        show this help message and exit
  --fast            compress faster
  --best            compress better
  -d, --decompress  act like gunzip instead of gzip

$ # ━━━ encoding ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
$ # uuencode
$ python3 -m uu < file
[...]
$ # base64 encode a string
$ echo "Hello" | python3 -m base64
SGVsbG8K
$ echo "SGVsbG8K" | python3 -m base64 -u
Hello
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment