Merge branch Print help when no subcommand is provided #100.
Fixes #99.
This commit is contained in:
commit
88a6921fad
@ -69,7 +69,6 @@ def execute(raw_args=sys.argv):
|
|||||||
prog="pubs", add_help=True)
|
prog="pubs", add_help=True)
|
||||||
parser.add_argument('--version', action='version', version=__version__)
|
parser.add_argument('--version', action='version', version=__version__)
|
||||||
subparsers = parser.add_subparsers(title="valid commands", dest="command")
|
subparsers = parser.add_subparsers(title="valid commands", dest="command")
|
||||||
subparsers.required = True
|
|
||||||
|
|
||||||
# Populate the parser with core commands
|
# Populate the parser with core commands
|
||||||
for cmd_name, cmd_mod in CORE_CMDS.items():
|
for cmd_name, cmd_mod in CORE_CMDS.items():
|
||||||
@ -83,8 +82,15 @@ def execute(raw_args=sys.argv):
|
|||||||
|
|
||||||
# Eventually autocomplete
|
# Eventually autocomplete
|
||||||
autocomplete(parser)
|
autocomplete(parser)
|
||||||
|
|
||||||
# Parse and run appropriate command
|
# Parse and run appropriate command
|
||||||
args = parser.parse_args(remaining_args)
|
# if no command, print help and exit peacefully (as '--help' does)
|
||||||
|
args = parser.parse_args(remaining_args)
|
||||||
|
if not args.command:
|
||||||
|
ui.error("Too few arguments!\n")
|
||||||
|
parser.print_help(file=sys.stderr)
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
args.prog = "pubs" # FIXME?
|
args.prog = "pubs" # FIXME?
|
||||||
args.func(conf, args)
|
args.func(conf, args)
|
||||||
|
|
||||||
|
@ -125,3 +125,4 @@ You can access the self-documented configuration by using `pubs conf`, and all t
|
|||||||
- [Olivier Mangin](http://olivier.mangin.com)
|
- [Olivier Mangin](http://olivier.mangin.com)
|
||||||
- Jonathan Grizou
|
- Jonathan Grizou
|
||||||
- Arnold Sykosch
|
- Arnold Sykosch
|
||||||
|
- [Bill Flynn](https://github.com/wflynny)
|
||||||
|
@ -90,7 +90,7 @@ class FakeInput():
|
|||||||
class TestFakeFs(fake_filesystem_unittest.TestCase):
|
class TestFakeFs(fake_filesystem_unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.rootpath = os.path.dirname(__file__)
|
self.rootpath = os.path.abspath(os.path.dirname(__file__))
|
||||||
self.setUpPyfakefs()
|
self.setUpPyfakefs()
|
||||||
self.fs.CreateDirectory(self.rootpath)
|
self.fs.CreateDirectory(self.rootpath)
|
||||||
os.chdir(self.rootpath)
|
os.chdir(self.rootpath)
|
||||||
|
@ -35,8 +35,15 @@ class FakeSystemExit(Exception):
|
|||||||
function, so they can be catched by ExpectedFailure tests in Python 2.x.
|
function, so they can be catched by ExpectedFailure tests in Python 2.x.
|
||||||
|
|
||||||
If a code is expected to raise SystemExit, catch FakeSystemExit instead.
|
If a code is expected to raise SystemExit, catch FakeSystemExit instead.
|
||||||
|
|
||||||
|
Added explicit __init__ so SystemExit.code functionality could be emulated.
|
||||||
|
Taking form from https://stackoverflow.com/a/26938914/1634191
|
||||||
"""
|
"""
|
||||||
pass
|
def __init__(self, message, code=None, *args):
|
||||||
|
self.message = message
|
||||||
|
self.code = code
|
||||||
|
|
||||||
|
super(FakeSystemExit, self).__init__(message, *args)
|
||||||
|
|
||||||
|
|
||||||
# code for fake fs
|
# code for fake fs
|
||||||
@ -131,9 +138,9 @@ class CommandTestCase(fake_env.TestFakeFs):
|
|||||||
exc_class, exc, tb = sys.exc_info()
|
exc_class, exc, tb = sys.exc_info()
|
||||||
if sys.version_info.major == 2:
|
if sys.version_info.major == 2:
|
||||||
# using six to avoid a SyntaxError in Python 3.x
|
# using six to avoid a SyntaxError in Python 3.x
|
||||||
six.reraise(FakeSystemExit, exc, tb)
|
six.reraise(FakeSystemExit, FakeSystemExit(*exc.args), tb)
|
||||||
else:
|
else:
|
||||||
raise FakeSystemExit(exc).with_traceback(tb)
|
raise FakeSystemExit(*exc.args).with_traceback(tb)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
pass
|
pass
|
||||||
@ -190,8 +197,27 @@ class URLContentTestCase(DataCommandTestCase):
|
|||||||
class TestAlone(CommandTestCase):
|
class TestAlone(CommandTestCase):
|
||||||
|
|
||||||
def test_alone_misses_command(self):
|
def test_alone_misses_command(self):
|
||||||
with self.assertRaises(FakeSystemExit):
|
with self.assertRaises(FakeSystemExit) as cm:
|
||||||
self.execute_cmds(['pubs'])
|
self.execute_cmds(['pubs'])
|
||||||
|
self.assertEqual(cm.exception.code, 2)
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skipIf(sys.version_info.major == 2, "not supported for Python2")
|
||||||
|
def test_alone_prints_help(self):
|
||||||
|
# capturing the output of `pubs --help` is difficult because argparse
|
||||||
|
# raises as SystemExit(0) after calling `print_help`, and this gets
|
||||||
|
# caught so no output is captured. so comparing outputs of `pubs` and
|
||||||
|
# `pubs --help` isn't too easy unless substantially reorganization of
|
||||||
|
# the parser and testing context is made. instead, the exit codes of
|
||||||
|
# the two usecases are compared.
|
||||||
|
|
||||||
|
with self.assertRaises(FakeSystemExit) as cm1:
|
||||||
|
self.execute_cmds(['pubs'])
|
||||||
|
|
||||||
|
with self.assertRaises(FakeSystemExit) as cm2:
|
||||||
|
self.execute_cmds(['pubs', '--help'])
|
||||||
|
|
||||||
|
self.assertEqual(cm1.exception.code, cm2.exception.code, 2)
|
||||||
|
|
||||||
|
|
||||||
class TestInit(CommandTestCase):
|
class TestInit(CommandTestCase):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user