Merge ppa-dev-tools:use-subparsers-for-args into ppa-dev-tools:main

Proposed by Bryce Harrington
Status: Merged
Approved by: Bryce Harrington
Approved revision: 37c7bb10017c75ab4f4f7d1b261bd085c6f4d0e9
Merge reported by: Bryce Harrington
Merged at revision: 37c7bb10017c75ab4f4f7d1b261bd085c6f4d0e9
Proposed branch: ppa-dev-tools:use-subparsers-for-args
Merge into: ppa-dev-tools:main
Diff against target: 179 lines (+120/-19)
2 files modified
scripts/ppa (+60/-9)
tests/test_scripts_ppa.py (+60/-10)
Reviewer Review Type Date Requested Status
Christian Ehrhardt  (community) Approve
Ubuntu Server Pending
Canonical Server Reporter Pending
Review via email: mp+431543@code.launchpad.net

Description of the change

Modifies command argument handling to allow each command to have its own set of options. I had been originally planning to have a single parser and just avoid overloading args, but doing this allows for say, -N to mean one thing for one command and something totally different for another.

Two other more practical effects of this is that it should make the help text better organized (so it'll be clearer for users which args are relevant for each command), as well as writing test cases.

To post a comment you must log in.
Revision history for this message
Christian Ehrhardt  (paelzer) wrote :

Generally ok with this split, with the low amount of suboptions it looks odd, but makes sense IMHO.
One request on better wording or fixing bugs or both (Sorry, didn't track their status if they are already fixed).

Plus one potentially wrong nit pick (you are the native speaker after all).

Either way, none of this is strong enough to stop this +1 overall.

review: Approve
Revision history for this message
Bryce Harrington (bryce) wrote :

Thanks, yeah the options will be growing this branch just sets the framework to place them in.

Revision history for this message
Bryce Harrington (bryce) wrote :

Pushed:
To git+ssh://git.launchpad.net/ppa-dev-tools
   8ec7d86..0e673cc main -> main

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/scripts/ppa b/scripts/ppa
index 1607342..ec66f03 100755
--- a/scripts/ppa
+++ b/scripts/ppa
@@ -130,14 +130,65 @@ def create_arg_parser():
130 action='version',130 action='version',
131 version='%(prog)s {version}'.format(version=__version__),131 version='%(prog)s {version}'.format(version=__version__),
132 help="Version information")132 help="Version information")
133 parser.add_argument('--dry-run',133 parser.add_argument('-v', '--verbose',
134 dest='dry_run', action='store_true',134 dest='verbose', action='store_true',
135 help="Simulate command without modifying anything")135 help="Print more information during processing")
136 parser.add_argument('ppa_name', metavar='ppa-name',136 parser.add_argument('-q', '--quiet',
137 action='store',137 dest='quiet', action='store_true',
138 help="Name of the PPA to be created")138 help="Minimize output during processing")
139 parser.add_argument('remainder',139
140 nargs=argparse.REMAINDER)140 subparser = parser.add_subparsers(dest='command')
141
142 # Create Command
143 create_parser = subparser.add_parser('create', help='create help')
144 create_parser.add_argument('ppa_name', metavar='ppa-name',
145 action='store',
146 help="Name of the PPA to be created")
147
148 # Desc Command
149 desc_parser = subparser.add_parser('desc', help='desc help')
150 desc_parser.add_argument('ppa_name', metavar='ppa-name',
151 action='store',
152 help="Name of the PPA to describe")
153 desc_parser.add_argument('description',
154 nargs=argparse.REMAINDER)
155
156 # Destroy Command
157 destroy_parser = subparser.add_parser('destroy', help='destroy help')
158 destroy_parser.add_argument('ppa_name', metavar='ppa-name',
159 action='store',
160 help="Name of the PPA to destroy")
161
162 # List Command
163 list_parser = subparser.add_parser('list', help='list help')
164 list_parser.add_argument('ppa_name', metavar='ppa-name',
165 action='store',
166 help="Name of the PPA to list")
167
168 # Show Command
169 show_parser = subparser.add_parser('show', help='show help')
170 show_parser.add_argument('ppa_name', metavar='ppa-name',
171 action='store',
172 help="Name of the PPA to show")
173
174 # Status Command
175 status_parser = subparser.add_parser('status', help='status help')
176 status_parser.add_argument('ppa_name', metavar='ppa-name',
177 action='store',
178 help="Name of the PPA to report status of")
179
180 # Tests Command
181 tests_parser = subparser.add_parser('tests', help='tests help')
182 tests_parser.add_argument('ppa_name', metavar='ppa-name',
183 action='store',
184 help="Name of the PPA to view tests")
185
186 # Wait Command
187 wait_parser = subparser.add_parser('wait', help='wait help')
188 wait_parser.add_argument('ppa_name', metavar='ppa-name',
189 action='store',
190 help="Name of the PPA to wait on")
191
141 return parser192 return parser
142193
143194
@@ -274,7 +325,7 @@ def command_desc(lp, config):
274 if not sys.stdin.isatty():325 if not sys.stdin.isatty():
275 description = sys.stdin.read()326 description = sys.stdin.read()
276 else:327 else:
277 description = ' '.join(config.get('remainder', None))328 description = ' '.join(config.get('description', None))
278329
279 if not description or len(description) < 3:330 if not description or len(description) < 3:
280 warn('No description provided')331 warn('No description provided')
diff --git a/tests/test_scripts_ppa.py b/tests/test_scripts_ppa.py
index d344340..a582fca 100644
--- a/tests/test_scripts_ppa.py
+++ b/tests/test_scripts_ppa.py
@@ -8,11 +8,14 @@
8# Released under GNU GPLv2 or later, read the file 'LICENSE.GPLv2+' for8# Released under GNU GPLv2 or later, read the file 'LICENSE.GPLv2+' for
9# more information.9# more information.
1010
11"""ppa command-line script tests"""
12
11import os13import os
12import sys14import sys
13import types15import types
1416
15import importlib.machinery17import importlib.machinery
18import argparse
16import pytest19import pytest
1720
18SCRIPT_NAME = 'ppa'21SCRIPT_NAME = 'ppa'
@@ -85,16 +88,63 @@ def fake_bug_report():
8588
8689
87def test_create_arg_parser():90def test_create_arg_parser():
88 # parser = script.create_arg_parser()91 """
8992 Checks that the main argument processor is created properly.
90 # TODO: Don't I have an existing example arg-parse test somewhere?93 It must support the top level options as well as the expected
91 # TODO: Check command is recognized94 set of subparsers.
92 # TODO: Check --config95
93 # TODO: Check --debug96 Note we don't test --help or --version since these are built-ins
94 # TODO: Check --version97 from argparse so we don't control their behavior. They also both
95 # TODO: Check --dry-run98 exit when done which is not conducive to testing.
96 # TODO: Check ppa_name99 """
97 # TODO: Check remainder100 parser = script.create_arg_parser()
101
102 # Check command is recognized
103 args = parser.parse_args(['test-command'])
104 assert args.command[0] == 'test-command'
105
106 # Check -D, --debug
107 args = parser.parse_args(['cmd', '-D'])
108 assert args.debug == True
109 args.debug = None
110 args = parser.parse_args(['cmd', '--debug'])
111 assert args.debug == True
112 args.debug = None
113
114 # Check -v, --verbose
115 args = parser.parse_args(['cmd', '-v'])
116 assert args.verbose == True
117 args.verbose = None
118 args = parser.parse_args(['cmd', '--verbose'])
119 assert args.verbose == True
120 args.verbose = None
121
122 # Check -q, --quiet
123 args = parser.parse_args(['cmd', '-q'])
124 assert args.quiet == True
125 args.quiet = None
126 args = parser.parse_args(['cmd', '--quiet'])
127 assert args.quiet == True
128 args.quiet = None
129
130 # Verify all expected subparsers are present
131 subparsers_actions = [
132 action for action in parser._actions
133 if isinstance(action, argparse._SubParsersAction)]
134 subparsers = []
135 for subparsers_action in subparsers_actions:
136 for choice, subparser in subparsers_action.choices.items():
137 subparsers.append(choice)
138 assert subparsers == [
139 'create',
140 'desc',
141 'destroy',
142 'list',
143 'show',
144 'status',
145 'tests',
146 'wait'
147 ]
98148
99 pass149 pass
100150

Subscribers

People subscribed via source and target branches

to all changes: