Nov-15-2022, 01:57 AM
(This post was last modified: Nov-15-2022, 01:57 AM by deanhystad.)
Command line parsing and verification is hard. Maybe not in this case, but quite often it can be hard. Use argparse if the command line is fairly simple. Use Click for a complicated command line interface. Both provide verification and even help (or --help).
A stab at using argparse to parse the 1 positional argument.
If you really want usage, that could be an optional argument. How is -u different from -h?
A stab at using argparse to parse the 1 positional argument.
import argparse def directory_watcher(directory): print("Watching", directory) if __name__ == "__main__": parser = argparse.ArgumentParser( prog="dir_watcher", description="Travel the directory and display names of all entries") parser.add_argument("directory", help="Directory to watch") args = parser.parse_args() directory_watcher(arg.directory)argparse automatically creates -h --help
If you really want usage, that could be an optional argument. How is -u different from -h?
Output:> python dir_watcher.py -h
usage: dir_watcher [-h] directory
Travel the directory and display names of all entries
positional arguments:
directory Directory to watch
optional arguments:
-h, --help show this help message and exit
>
>
> python dir_watcher.py stuff
Watching stuff