Argparse: expected one argument

If you use the argument -l on the cli you need to specify an argument, like:

python macc.py -l something

If you set default = 1 on the -l argument you can run your script without using it like this:

python macc.py

The default action for an argument is store, which sets the value of the attribute in the namespace returned by parser.parse_args using the next command line argument.

You don't want to store any particular value; you just want to acknowledge that -l was used. A quick hack would be to use the store_true action (which would set args.list to True).

parser = argparse.ArgumentParser(prog='macc')
parser.add_argument('-l', '--list', action='store_true', help='Lists MAC Addresses')

args = parser.parse_args()

if args.list:
    list_macs()

The store_true action implies type=bool and default=False as well.


However, a slightly cleaner approach would be to define a subcommand named list. With this approach, your invocation would be macc.py list rather than macc.py --list.

parser = argparse.ArgumentParser(prog='macc')
subparsers = parser.add_subparsers(dest='cmd_name')
subparsers.add_parser('list')

args = parser.parse_args()

if args.cmd_name == "list":
    list_macs()