Muffi
Muffi

Reputation: 55

python argparse stopped working

I had a perfectly fine and working piece of code, which used argparse. I've been using it for months for work without any issue. Below is an excrept.

import argparse
import sys
import math
import random

# Setup command line arguments
parser = argparse.ArgumentParser(description='RF Profile Generator', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-o', dest='OutputFile', help='Output filename', required=True, type=argparse.FileType('w', encoding='utf-8'))
parser.add_argument('-a', dest='APPositions', nargs='+', type=float, help='Trackside AP position(s)', required=True)
parser.add_argument('-l', dest='TotalTrackLength', type=float, help='Length of the track (m)', required=True)
parser.add_argument('-p', dest='AttenuatorAddr', nargs='+', help='Programmable attenuator IP address(es)', required=True)

input("A")
# parse the command line arguments
# the parsed values will be stored in the corresponding variables defined by 'dest'
args = parser.parse_args()
input("B")
#More code to follow

However, I ran the code today with the following arguments (which i have been doing all along):

rf.py -o OutputFile -a 10 20 30 40 -l 600 -p 10.0.1.55

and the console output tells me

"error: the following arguments are required: -o, -a, -l, -p"

I do not understand why this has stopped working. To troubleshoot, i added 2 input commands, but the code never reaches input("B") Could someone please advise me on a possible reason for this to happen. The version of python is 3.4.1, and has not been updated between the last time i ran this code and now.

Thank you very much

Upvotes: 2

Views: 1747

Answers (1)

holdenweb
holdenweb

Reputation: 37053

Since required=True is present on all the calls to add_argument, my conclusion is that either

  • somebody else has changed your program, or
  • this is the first time you have tried to run it without providing those arguments

I do, however, find it odd that running your program with the --help option gives the following output:

usage: so16.py [-h] -o OUTPUTFILE -a APPOSITIONS [APPOSITIONS ...] -l
               TOTALTRACKLENGTH -p ATTENUATORADDR [ATTENUATORADDR ...]

RF Profile Generator

optional arguments:
  -h, --help            show this help message and exit
  -o OUTPUTFILE         Output filename (default: None)
  -a APPOSITIONS [APPOSITIONS ...]
                        Trackside AP position(s) (default: None)
  -l TOTALTRACKLENGTH   Length of the track (m) (default: None)
  -p ATTENUATORADDR [ATTENUATORADDR ...]
                        Programmable attenuator IP address(es) (default: None)

I'm puzzled as to why the message implies that required arguments are optional.

Upvotes: 1

Related Questions