Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

bmaptools/CLI.py: Print help in case of no arguments#51

Closed
lsandov1 wants to merge 1 commit intointel:masterfrom
lsandov1:fix-empty-args
Closed

bmaptools/CLI.py: Print help in case of no arguments#51
lsandov1 wants to merge 1 commit intointel:masterfrom
lsandov1:fix-empty-args

Conversation

@lsandov1
Copy link

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'

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'
@kad
Copy link
Member

kad commented Jun 18, 2018

I'm curious, in which environment you got such traceback ? and how it was installed ?

@codecov-io
Copy link

codecov-io commented Jun 18, 2018

Codecov Report

Merging #51 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff           @@
##           master     #51   +/-   ##
======================================
  Coverage    67.7%   67.7%           
======================================
  Files           5       5           
  Lines        1025    1025           
======================================
  Hits          694     694           
  Misses        331     331

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 18291e2...ac01eb4. Read the comment docs.

@icarus-sparry
Copy link

@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.

@lsandov1
Copy link
Author

Clear Linux OS and the installation is done through https://github.com/clearlinux-pkgs/bmap-tools

@smcv
Copy link
Contributor

smcv commented Jun 18, 2018

This seems to be a behaviour change in Python 3:

$ python2 ./bmaptool
usage: bmaptool [-h] [--version] [-q] [-d] {create,copy} ...
bmaptool: error: too few arguments
$ python3 ./bmaptool
Traceback (most recent call last):
  File "./bmaptool", line 11, in <module>
    sys.exit(main())
  File "/home/smcv/src/debian/bmap-tools/bmaptools/CLI.py", line 715, in main
    args.func(args)
AttributeError: 'Namespace' object has no attribute 'func'

@smcv
Copy link
Contributor

smcv commented Jun 18, 2018

See also python/cpython#3027, python/cpython#6919.

@smcv
Copy link
Contributor

smcv commented Jun 18, 2018

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):

@smcv
Copy link
Contributor

smcv commented Jun 18, 2018

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

@lsandov1
Copy link
Author

lsandov1 commented Jun 18, 2018

Right, your approach is better

@lsandov1 lsandov1 closed this Jun 19, 2018
@lsandov1
Copy link
Author

I will send a v2.

lsandov1 pushed a commit to lsandov1/bmap-tools that referenced this pull request Jun 19, 2018
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)]
lsandov1 pushed a commit to lsandov1/bmap-tools that referenced this pull request Jun 19, 2018
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)]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants