Home > Software design >  argparse is matching to the closest between the command line arguments and arguments added by the sc
argparse is matching to the closest between the command line arguments and arguments added by the sc

Time:01-02

I have a few arguments added to my argparse parser in my script as below:

parser = argparse.ArgumentParser()
parser.add_argument("--eval_model_dir", type=str, default='',
                    help="Model directory for evaluation.")
parser.add_argument("--evaluate_during_training", action='store_true',
                    help="Run evaluation during training at each save_steps.")

I am running the script from command line as below:

python test.py --eval_ dummy_value

Even when the argument passed from the command line, --eval_, does not match the argument in the script, --eval_model_dir, the value passed, dummy_value, is assigned to --eval_model_dir. If the command line argument is --eval, I get an error message as following: error: ambiguous option: --eval could match --evaluate_during_training, --eval_model_dir.

Based on my reading of the Argparse official documentation, I did not find it mentioned that the command line arguments and the script arguments could be the closest match. Intuitively, I would think that an exact match would be needed. I have following questions:

  • Is this matching with the closest argument feature of argparse? If so, if someone could please point me to the official documentation for that.
  • What are the rules for this matching? Based on the runs that I have done, arguments are matched when reading the command line argument from left to right can fit in only one of the arguments in the script. Not sure if this is correct or there are any corner cases in this matching.
  • Is there a way to stop his closest matching?

CodePudding user response:

From the official doc you linked: https://docs.python.org/3/library/argparse.html#argument-abbreviations-prefix-matching

CodePudding user response:

Please refer Argument abbreviations (prefix matching) And you can disable it by setting "allow_abbrev" to False

CodePudding user response:

From the documentation:

class argparse.ArgumentParser(..., allow_abbrev=True, ...)

...

  • allow_abbrev - Allows long options to be abbreviated if the abbreviation is unambiguous. (default: True)

More info: Argument abbreviations (prefix matching)

  • Related