Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

argparse is matching to the closest between the command line arguments and arguments added by the script

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?
like image 814
Gaurav Srivastava Avatar asked Oct 14 '25 07:10

Gaurav Srivastava


1 Answers

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

like image 111
snapshoe Avatar answered Oct 18 '25 05:10

snapshoe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!