0

Answer is in the following, which was suggested by a welcomed viewer. Python argparse: default value or specified value

I need help with overwriting my title from the shell when I run the python program. So far, I just hard code the name inside of the program as below. But, I need an option called -title which should follow with the new title. It should be an optional option, so if no -title when running the program, the current title should stay the same. Example would be like:

>python Graph.py -title "New Title"

This should overwrite the old title with the new one. But if I type just

>python Graph.py

It should keep the title as "Old Title".

I surfed the internet and found that I need argument with parser or string. I think using string with argument would work but I am not sure how to make the option optional, so if there is not -title, the title should stay the same.

Right now, I have the following hard coded

plt.title("\n".join(wrap("Performance for "+file1+' & '+file2 +' & '+file3)), size = 12)

r_e
  • 242
  • 4
  • 14
  • 1
    The duplicate target is about `argparse` library, but I personally prefer [`click`](https://click.palletsprojects.com/en/7.x/). The example on the main page includes default arguments. – Georgy Jul 10 '19 at 14:02
  • He @Georgy, how can I use the click for an optional argument? Given example in the main page is for positional. Could help help me out with it? – r_e Jul 10 '19 at 14:10
  • Not sure what you mean by "positional" in this context. The `--count` argument is optional in that example as you want it to be. You can see more examples [here](https://click.palletsprojects.com/en/7.x/options/), maybe they will be more helpful. I suggest playing with the examples they provide to get the hang of it, and if there will be something unclear, you can ask a new question here. – Georgy Jul 10 '19 at 14:25

0 Answers0