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
1diff --git a/scripts/ppa b/scripts/ppa
2index 1607342..ec66f03 100755
3--- a/scripts/ppa
4+++ b/scripts/ppa
5@@ -130,14 +130,65 @@ def create_arg_parser():
6 action='version',
7 version='%(prog)s {version}'.format(version=__version__),
8 help="Version information")
9- parser.add_argument('--dry-run',
10- dest='dry_run', action='store_true',
11- help="Simulate command without modifying anything")
12- parser.add_argument('ppa_name', metavar='ppa-name',
13- action='store',
14- help="Name of the PPA to be created")
15- parser.add_argument('remainder',
16- nargs=argparse.REMAINDER)
17+ parser.add_argument('-v', '--verbose',
18+ dest='verbose', action='store_true',
19+ help="Print more information during processing")
20+ parser.add_argument('-q', '--quiet',
21+ dest='quiet', action='store_true',
22+ help="Minimize output during processing")
23+
24+ subparser = parser.add_subparsers(dest='command')
25+
26+ # Create Command
27+ create_parser = subparser.add_parser('create', help='create help')
28+ create_parser.add_argument('ppa_name', metavar='ppa-name',
29+ action='store',
30+ help="Name of the PPA to be created")
31+
32+ # Desc Command
33+ desc_parser = subparser.add_parser('desc', help='desc help')
34+ desc_parser.add_argument('ppa_name', metavar='ppa-name',
35+ action='store',
36+ help="Name of the PPA to describe")
37+ desc_parser.add_argument('description',
38+ nargs=argparse.REMAINDER)
39+
40+ # Destroy Command
41+ destroy_parser = subparser.add_parser('destroy', help='destroy help')
42+ destroy_parser.add_argument('ppa_name', metavar='ppa-name',
43+ action='store',
44+ help="Name of the PPA to destroy")
45+
46+ # List Command
47+ list_parser = subparser.add_parser('list', help='list help')
48+ list_parser.add_argument('ppa_name', metavar='ppa-name',
49+ action='store',
50+ help="Name of the PPA to list")
51+
52+ # Show Command
53+ show_parser = subparser.add_parser('show', help='show help')
54+ show_parser.add_argument('ppa_name', metavar='ppa-name',
55+ action='store',
56+ help="Name of the PPA to show")
57+
58+ # Status Command
59+ status_parser = subparser.add_parser('status', help='status help')
60+ status_parser.add_argument('ppa_name', metavar='ppa-name',
61+ action='store',
62+ help="Name of the PPA to report status of")
63+
64+ # Tests Command
65+ tests_parser = subparser.add_parser('tests', help='tests help')
66+ tests_parser.add_argument('ppa_name', metavar='ppa-name',
67+ action='store',
68+ help="Name of the PPA to view tests")
69+
70+ # Wait Command
71+ wait_parser = subparser.add_parser('wait', help='wait help')
72+ wait_parser.add_argument('ppa_name', metavar='ppa-name',
73+ action='store',
74+ help="Name of the PPA to wait on")
75+
76 return parser
77
78
79@@ -274,7 +325,7 @@ def command_desc(lp, config):
80 if not sys.stdin.isatty():
81 description = sys.stdin.read()
82 else:
83- description = ' '.join(config.get('remainder', None))
84+ description = ' '.join(config.get('description', None))
85
86 if not description or len(description) < 3:
87 warn('No description provided')
88diff --git a/tests/test_scripts_ppa.py b/tests/test_scripts_ppa.py
89index d344340..a582fca 100644
90--- a/tests/test_scripts_ppa.py
91+++ b/tests/test_scripts_ppa.py
92@@ -8,11 +8,14 @@
93 # Released under GNU GPLv2 or later, read the file 'LICENSE.GPLv2+' for
94 # more information.
95
96+"""ppa command-line script tests"""
97+
98 import os
99 import sys
100 import types
101
102 import importlib.machinery
103+import argparse
104 import pytest
105
106 SCRIPT_NAME = 'ppa'
107@@ -85,16 +88,63 @@ def fake_bug_report():
108
109
110 def test_create_arg_parser():
111- # parser = script.create_arg_parser()
112-
113- # TODO: Don't I have an existing example arg-parse test somewhere?
114- # TODO: Check command is recognized
115- # TODO: Check --config
116- # TODO: Check --debug
117- # TODO: Check --version
118- # TODO: Check --dry-run
119- # TODO: Check ppa_name
120- # TODO: Check remainder
121+ """
122+ Checks that the main argument processor is created properly.
123+ It must support the top level options as well as the expected
124+ set of subparsers.
125+
126+ Note we don't test --help or --version since these are built-ins
127+ from argparse so we don't control their behavior. They also both
128+ exit when done which is not conducive to testing.
129+ """
130+ parser = script.create_arg_parser()
131+
132+ # Check command is recognized
133+ args = parser.parse_args(['test-command'])
134+ assert args.command[0] == 'test-command'
135+
136+ # Check -D, --debug
137+ args = parser.parse_args(['cmd', '-D'])
138+ assert args.debug == True
139+ args.debug = None
140+ args = parser.parse_args(['cmd', '--debug'])
141+ assert args.debug == True
142+ args.debug = None
143+
144+ # Check -v, --verbose
145+ args = parser.parse_args(['cmd', '-v'])
146+ assert args.verbose == True
147+ args.verbose = None
148+ args = parser.parse_args(['cmd', '--verbose'])
149+ assert args.verbose == True
150+ args.verbose = None
151+
152+ # Check -q, --quiet
153+ args = parser.parse_args(['cmd', '-q'])
154+ assert args.quiet == True
155+ args.quiet = None
156+ args = parser.parse_args(['cmd', '--quiet'])
157+ assert args.quiet == True
158+ args.quiet = None
159+
160+ # Verify all expected subparsers are present
161+ subparsers_actions = [
162+ action for action in parser._actions
163+ if isinstance(action, argparse._SubParsersAction)]
164+ subparsers = []
165+ for subparsers_action in subparsers_actions:
166+ for choice, subparser in subparsers_action.choices.items():
167+ subparsers.append(choice)
168+ assert subparsers == [
169+ 'create',
170+ 'desc',
171+ 'destroy',
172+ 'list',
173+ 'show',
174+ 'status',
175+ 'tests',
176+ 'wait'
177+ ]
178
179 pass
180

Subscribers

People subscribed via source and target branches

to all changes: