bmaptools/CLI.py: Print help in case of no arguments#51
bmaptools/CLI.py: Print help in case of no arguments#51lsandov1 wants to merge 1 commit intointel:masterfrom
Conversation
This fix avoids the following run-time exception:
$ bmaptool
Traceback (most recent call last):
File "/usr/bin/bmaptool", line 11, in <module>
load_entry_point('bmap-tools==3.4', 'console_scripts', 'bmaptool')()
File "/usr/lib/python3.6/site-packages/bmaptools/CLI.py", line 715, in main
args.func(args)
AttributeError: 'Namespace' object has no attribute 'func'
|
I'm curious, in which environment you got such traceback ? and how it was installed ? |
Codecov Report
@@ Coverage Diff @@
## master #51 +/- ##
======================================
Coverage 67.7% 67.7%
======================================
Files 5 5
Lines 1025 1025
======================================
Hits 694 694
Misses 331 331Continue to review full report at Codecov.
|
|
@kad We see this in both debian and clear linux, the former with the standard repository. The reproduction step is just to invoke {code}bmaptool{code} without any arguments. If you give it an argument then it prints out a help message if you do not give it one of the two required subcommands. |
|
Clear Linux OS and the installation is done through https://github.com/clearlinux-pkgs/bmap-tools |
|
This seems to be a behaviour change in Python 3: |
|
See also python/cpython#3027, python/cpython#6919. |
|
This might be a nicer way to do the same thing? diff --git a/bmaptools/CLI.py b/bmaptools/CLI.py
index 09b23af..cad4173 100644
--- a/bmaptools/CLI.py
+++ b/bmaptools/CLI.py
@@ -636,7 +636,12 @@ def parse_arguments():
text = "do not verify the data checksum while writing"
parser_copy.add_argument("--no-verify", action="store_true", help=text)
- return parser.parse_args()
+ args = parser.parse_args()
+
+ if not hasattr(args, 'func'):
+ parser.error('too few arguments')
+
+ return args
def setup_logger(loglevel): |
|
Or even: diff --git a/bmaptools/CLI.py b/bmaptools/CLI.py
index 09b23af..eb694e1 100644
--- a/bmaptools/CLI.py
+++ b/bmaptools/CLI.py
@@ -579,7 +579,8 @@ def parse_arguments():
text = "print debugging information"
parser.add_argument("-d", "--debug", action="store_true", help=text)
- subparsers = parser.add_subparsers(title="commands")
+ subparsers = parser.add_subparsers(title="commands", dest="command")
+ subparsers.required = True
#
# Create parser for the "create" command |
|
Right, your approach is better |
|
I will send a v2. |
This fix avoids the following run-time exception:
$ bmaptool
Traceback (most recent call last):
File "/usr/bin/bmaptool", line 11, in <module>
load_entry_point('bmap-tools==3.4', 'console_scripts', 'bmaptool')()
File "/usr/lib/python3.6/site-packages/bmaptools/CLI.py", line 715, in main
args.func(args)
AttributeError: 'Namespace' object has no attribute 'func'
Suggested by: Simon McVittie [intel#51 (comment)]
Avoids the run-time exception on systems with python3 as the default
python version (python2 requires a subparser so it fails on empty args):
$ bmaptool
Traceback (most recent call last):
File "/usr/bin/bmaptool", line 11, in <module>
load_entry_point('bmap-tools==3.4', 'console_scripts', 'bmaptool')()
File "/usr/lib/python3.6/site-packages/bmaptools/CLI.py", line 715, in main
args.func(args)
AttributeError: 'Namespace' object has no attribute 'func'
Suggested by: Simon McVittie [intel#51 (comment)]
This fix avoids the following run-time exception: