Skip to content

Commit 1a39f14

Browse files
authored
Manof arg parser tidy (#27)
* Manof arg parser tidy * Image command -> provision command * Fixed lift args
1 parent a35eaa3 commit 1a39f14

File tree

1 file changed

+72
-82
lines changed

1 file changed

+72
-82
lines changed

manof.py

Lines changed: 72 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,42 @@ def _register_arguments(parser):
7373
# update
7474
subparsers.add_parser('update', help='Updates Manof')
7575

76+
# base sub parser
77+
base_command_parent_parser = argparse.ArgumentParser(add_help=False)
78+
base_command_parent_parser.add_argument('targets', nargs='+')
79+
base_command_parent_parser.add_argument('-e',
80+
'--exclude',
81+
help='Exclude targets when running manof cmd (comma-delimited, no spaces)',
82+
default='')
83+
84+
# TODO: Change default to 'docker.io'. Currently default is None for backwards compatibility
85+
base_command_parent_parser.add_argument('-r',
86+
'--repository',
87+
help='The repository from which images shall be taken from or pushed to',
88+
default=None)
89+
90+
# image based commands
91+
provision_parent_command = argparse.ArgumentParser(add_help=False)
92+
provision_parent_command.add_argument('-n', '--no-cache',
93+
help='Don\'t use cache images on build',
94+
action='store_true')
95+
provision_parent_command.add_argument('--force-rm',
96+
help='Image: Always remove intermediate containers. '
97+
'NamedVolume: Delete existing before creation',
98+
action='store_true')
99+
provision_parent_command.add_argument('-tl',
100+
'--skip-tag-local',
101+
help='If no context is given, provision will perform pull and '
102+
'skip tagging the image with its local repository (default: False)',
103+
dest='tag_local',
104+
action='store_false')
105+
76106
# provision
77-
provision_command = subparsers.add_parser('provision', help='Build or pull target images')
78-
provision_command.add_argument('targets', nargs='+')
79-
provision_command.add_argument('-n', '--no-cache', help='Don\'t use cache images on build', action='store_true')
80-
provision_command.add_argument('--force-rm',
81-
help='Image: Always remove intermediate containers. '
82-
'NamedVolume: Delete existing before creation',
83-
action='store_true')
84-
provision_command.add_argument('-tl',
85-
'--skip-tag-local',
86-
help='If no context is given, provision will perform pull and '
87-
'skip tagging the image with its local repository (default: False)',
88-
dest='tag_local',
89-
action='store_false')
107+
subparsers.add_parser('provision',
108+
help='Build or pull target images',
109+
parents=[base_command_parent_parser, provision_parent_command])
90110

91111
run_parent_parser = argparse.ArgumentParser(add_help=False)
92-
run_parent_parser.add_argument('targets', nargs='+')
93112
run_parent_parser.add_argument('--privileged',
94113
action='store_true',
95114
help='Give extended privileges to these containers')
@@ -110,101 +129,72 @@ def _register_arguments(parser):
110129
help='Limit write rate (IO per second) to a device (e.g. /dev/sda:50)')
111130
run_parent_parser.add_argument('--cap-add', help='Add capability to the container', action='append')
112131
run_parent_parser.add_argument('--cap-drop', help='Drop capability from the container', action='append')
132+
run_parent_parser.add_argument('-dv',
133+
'--delete-volumes',
134+
help='Image: Delete named_volumes that are used by this image',
135+
action='store_true')
136+
run_parent_parser.add_argument('-pco',
137+
'--print-command-only',
138+
help='Will enforce dry run and print the run command only, no logs at all',
139+
action='store_true')
113140

114141
# run
115-
run_command = subparsers.add_parser('run', help='Run target containers', parents=[run_parent_parser])
116-
run_command.add_argument('-dv',
117-
'--delete-volumes',
118-
help='Image: Delete named_volumes that are used by this image',
119-
action='store_true')
120-
run_command.add_argument('-pco',
121-
'--print-command-only',
122-
help='Will enforce dry run and print the run command only, no logs at all',
123-
action='store_true')
142+
subparsers.add_parser('run',
143+
help='Run target containers',
144+
parents=[base_command_parent_parser, run_parent_parser])
124145

125146
# stop
126-
stop_command = subparsers.add_parser('stop', help='Stop target containers')
147+
stop_command = subparsers.add_parser('stop',
148+
help='Stop target containers',
149+
parents=[base_command_parent_parser])
127150
stop_command.add_argument('-t',
128151
'--time',
129152
help='Seconds to wait for stop before killing it (default=10)',
130153
type=int,
131154
default=10)
132-
stop_command.add_argument('targets', nargs='+')
133155

134156
# rm
135-
rm_command = subparsers.add_parser('rm', help='Remove targets')
157+
rm_command = subparsers.add_parser('rm',
158+
help='Remove targets',
159+
parents=[base_command_parent_parser])
136160
rm_command.add_argument('-f', '--force', help='Kill targets even if they are running', action='store_true')
137161
rm_command.add_argument('-v',
138162
'--volumes',
139163
help='Remove the volumes associated with the container',
140164
action='store_true')
141-
rm_command.add_argument('targets', nargs='+')
142-
143-
# lift
144-
lift_command = subparsers.add_parser('lift', help='Provision and run targets', parents=[run_parent_parser])
145-
146-
# TODO: Make a more pluginable args parser. (Copied lift's args from run and provision)
147-
lift_command.add_argument('-dv',
148-
'--delete-volumes',
149-
help='Image: Delete named_volumes that are used by this image',
150-
action='store_true')
151-
lift_command.add_argument('-pco',
152-
'--print-command-only',
153-
help='Will enforce dry run and print the run command only, no logs at all',
154-
action='store_true')
155-
lift_command.add_argument('-n', '--no-cache', help='Don\'t use cache images on build', action='store_true')
156-
lift_command.add_argument('--force-rm',
157-
help='Image: Always remove intermediate containers. '
158-
'NamedVolume: Delete existing before creation',
159-
action='store_true')
160-
lift_command.add_argument('-tl',
161-
'--skip-tag-local',
162-
help='If no context is given, provision will perform pull and '
163-
'skip tagging the image with its local repository (default: False)',
164-
dest='tag_local',
165-
action='store_false')
166165

167166
# serialize
168-
serialize_command = subparsers.add_parser('serialize', help='Get a JSON representation of the targets')
169-
serialize_command.add_argument('targets', nargs='+')
167+
subparsers.add_parser('serialize',
168+
help='Get a JSON representation of the targets',
169+
parents=[base_command_parent_parser])
170170

171171
# push
172-
push_command = subparsers.add_parser('push', help='Push targets')
173-
push_command.add_argument('targets', nargs='+')
172+
push_command = subparsers.add_parser('push',
173+
help='Push targets',
174+
parents=[base_command_parent_parser])
174175
push_command.add_argument('-nc',
175176
'--no-cleanup',
176177
help='After pushing, delete the tagged image created to push',
177178
action='store_true')
178179

179180
# pull
180-
pull_command = subparsers.add_parser('pull', help='Pull targets')
181-
pull_command.add_argument('targets', nargs='+')
182-
pull_command.add_argument('-tl',
183-
'--tag-local',
184-
help='After pulling, tag the image with its local repository',
185-
action='store_true')
181+
pull_parent_parser = argparse.ArgumentParser(add_help=False)
182+
pull_parent_parser.add_argument('-tl',
183+
'--tag-local',
184+
help='After pulling, tag the image with its local repository',
185+
action='store_true')
186+
subparsers.add_parser('pull',
187+
help='Pull targets',
188+
parents=[base_command_parent_parser, pull_parent_parser])
186189

187-
# options common to all commands:
188-
for cmd_parse in [
189-
provision_command,
190-
run_command,
191-
stop_command,
192-
rm_command,
193-
lift_command,
194-
push_command,
195-
pull_command,
196-
serialize_command
197-
]:
198-
cmd_parse.add_argument('-e',
199-
'--exclude',
200-
help='Exclude targets when running manof cmd (comma-delimited, no spaces)',
201-
default='')
202-
203-
# TODO: Change default to 'docker.io'. Currently default is None for backwards compatibility
204-
cmd_parse.add_argument('-r',
205-
'--repository',
206-
help='The repository from which images shall be taken from or pushed to',
207-
default=None)
190+
# lift
191+
subparsers.add_parser('lift',
192+
help='Provision and run targets',
193+
parents=[
194+
base_command_parent_parser,
195+
provision_parent_command,
196+
run_parent_parser,
197+
])
208198

209199
known_option_strings = parser._option_string_actions.keys()
210200

0 commit comments

Comments
 (0)