Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prog option to argparse for help messages #600

Merged
merged 1 commit into from
Aug 8, 2023
Merged

Add prog option to argparse for help messages #600

merged 1 commit into from
Aug 8, 2023

Conversation

jack-champagne
Copy link
Contributor

@jack-champagne jack-champagne commented Aug 7, 2023

When running cantools, depending on how it is launched, the help message will give different program names after the word usage: which can be set to a distinct name. Instead of deferring this to the default (os.path.basename(sys.argv[0])) in argparse, specify program name explicitly in argparse options.

Before:

$ python -m cantools --help
usage: __main__.py [-h] [-d] [--version] {decode,monitor,plot,convert,dump,generate_c_source,list} ...

Various CAN utilities.
...

(pip installed)

$ cantools --help
usage: cantools [-h] [-d] [--version]
                {decode,monitor,dump,convert,generate_c_source} ...

Various CAN utilities.
...

After:

$ python -m cantools --help
usage: cantools [-h] [-d] [--version] {decode,monitor,plot,convert,dump,generate_c_source,list} ...

Various CAN utilities.
...

(pip installed)

$ cantools --help
usage: cantools [-h] [-d] [--version] {decode,monitor,plot,convert,dump,generate_c_source,list} ...

Various CAN utilities.
...

When running cantools, depending on how it is launched, the help message
will give different program names after the word `usage: `. Instead
of deferring this the default (os.path.basename(sys.argv[0])), specify
program name explicitly in argparse options.

Signed-off-by: Jack Champagne <jackchampagne.r@gmail.com>
@andlaus
Copy link
Member

andlaus commented Aug 7, 2023

hm, I'm only 90% sure that this is an improvement: if cantools is called in non-standard ways, the usage message would be off if is done like this. currently it is correct:

ln -s ~/.local/bin/cantools ct
./ct --help
usage: ct [-h] [-d] [--version] {generate_c_source,convert,list,dump,plot,decode,monitor} ...
[...]

I think this is pretty much a corner case, though. any opinions @zariiii9003, @juleq?

@jack-champagne
Copy link
Contributor Author

jack-champagne commented Aug 8, 2023

This is a good point, maybe instead of my change, just a check to avoid it showing __main__.py when launched this way. I see the benefit of it showing the exec name. What do you think?

@andlaus
Copy link
Member

andlaus commented Aug 8, 2023

I guess it would be a good solution. That said, I think that the current patch is acceptable if @zariiii9003 and @juleq do not object...

@zariiii9003
Copy link
Collaborator

I guess it would be a good solution. That said, I think that the current patch is acceptable if @zariiii9003 and @juleq do not object...

I do not object, looks okay to me.

@andlaus
Copy link
Member

andlaus commented Aug 8, 2023

alright, let's go for it. Merging, thanks @jack-champagne

@andlaus andlaus merged commit a437d6e into cantools:master Aug 8, 2023
16 checks passed
@jack-champagne jack-champagne deleted the fix/argparse-prog-naming branch August 9, 2023 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants