Use argparse in shell
Instead of using custom parsing and validation of args, use Python built-in module argparse. Change-Id: I2b44574d8df4c95792eb11c41bfabe71a37fdf65
This commit is contained in:
parent
ea43034f71
commit
4e8adf65a3
@ -11,6 +11,7 @@
|
||||
# under the License.
|
||||
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
|
||||
@ -40,19 +41,20 @@ e.g.
|
||||
|
||||
class ParserShell(object):
|
||||
|
||||
def _validate(self, args):
|
||||
if len(args) < 1:
|
||||
msg = _('The program requires a template or a CSAR file as an '
|
||||
'argument. Please refer to the usage documentation.')
|
||||
raise ValueError(msg)
|
||||
if "--template-file=" not in args[0]:
|
||||
msg = _('The program expects "--template-file" as the first '
|
||||
'argument. Please refer to the usage documentation.')
|
||||
raise ValueError(msg)
|
||||
def get_parser(self, argv):
|
||||
parser = argparse.ArgumentParser(prog="tosca-parser")
|
||||
|
||||
def main(self, args):
|
||||
self._validate(args)
|
||||
path = args[0].split('--template-file=')[1]
|
||||
parser.add_argument('--template-file',
|
||||
metavar='<filename>',
|
||||
required=True,
|
||||
help=_('YAML template or CSAR file to parse.'))
|
||||
|
||||
return parser
|
||||
|
||||
def main(self, argv):
|
||||
parser = self.get_parser(argv)
|
||||
(args, extra_args) = parser.parse_known_args(argv)
|
||||
path = args.template_file
|
||||
if os.path.isfile(path):
|
||||
self.parse(path)
|
||||
elif toscaparser.utils.urlutils.UrlUtils.validate_url(path):
|
||||
|
@ -29,16 +29,10 @@ class ShellTest(TestCase):
|
||||
"data/test_multiple_validation_errors.yaml")
|
||||
|
||||
def test_missing_arg(self):
|
||||
error = self.assertRaises(ValueError, shell.main, '')
|
||||
err_msg = _('The program requires a template or a CSAR file as an '
|
||||
'argument. Please refer to the usage documentation.')
|
||||
self.assertEqual(err_msg, str(error))
|
||||
self.assertRaises(SystemExit, shell.main, '')
|
||||
|
||||
def test_invalid_arg(self):
|
||||
error = self.assertRaises(ValueError, shell.main, 'parse me')
|
||||
err_msg = _('The program expects "--template-file" as the first '
|
||||
'argument. Please refer to the usage documentation.')
|
||||
self.assertEqual(err_msg, str(error))
|
||||
self.assertRaises(SystemExit, shell.main, 'parse me')
|
||||
|
||||
def test_template_not_exist(self):
|
||||
error = self.assertRaises(
|
||||
|
Loading…
x
Reference in New Issue
Block a user