16

I have the following

import argparse
parser = argparse.ArgumentParser(prog='macc', usage='macc [options] [address]')
parser.add_argument('-l', '--list', help='Lists MAC Addresses')
args = parser.parse_args()
print(args)
def list_macs():
 print("Found the following MAC Addresses")

I get an error when running with python macc.py -l it says that an argument was expected. Even when I change my code to parser.add_argument('-l', '--list', help='Lists MAC Addresses' default=1) I get the same error.

wjandrea
34.1k10 gold badges69 silver badges107 bronze badges
asked Dec 16, 2019 at 19:44
2
  • I am not an expert on argparse, but what it is saying is that there must be a value after "-l" option. If you run it using 'macc.py -l address_1' (or whatever value), it works! Commented Dec 16, 2019 at 19:52
  • 1
    The problem is clearer if you don't override the usage: macc [-h] [-l LIST] Commented Dec 16, 2019 at 20:36

3 Answers 3

35

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()
answered Dec 16, 2019 at 19:48
Sign up to request clarification or add additional context in comments.

Comments

2

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
answered Dec 16, 2019 at 19:52

Comments

1

You could solve this by setting a default value and set nargs='?'.

If nothing is provided after -l, the default value is saved for the argument. It would look something like this:

parser.add_argument('-l', '--list', help='Lists MAC Addresses', default=1, nargs='?')

More info: https://docs.python.org/3/library/argparse.html#nargs

answered Feb 23, 2024 at 12:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.