1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 | diff --git a/cloudinit/shell.py b/cloudinit/shell.py
index 2a32ba9..c7c9a37 100644
--- a/cloudinit/shell.py
+++ b/cloudinit/shell.py
@@ -7,22 +7,33 @@ import argparse
import sys
-def populate_parser(parser, common, subcommands, func_namespace=None):
- if func_namespace is None:
- func_namespace = globals()
+def populate_parser(parser, common, subcommands):
+ """A data driven approach to populating ArgumentParser
+ :param parser:
+ the :py:mod:`argparse.ArgumentParser` to populate.
+
+ :param common:
+ a :py:func:`list` of tuples. Each tuple is args and kwargs that are
+ passed onto :py:func:`argparse.ArgumentParser.add_argument`
+
+ :param subcommands:
+ a :py:func:dict of subcommands to add.
+ The key is added as the subcommand name.
+ 'func' is called to implement the subcommand.
+ 'help' is set up as the subcommands help message
+ entries in 'opts' are passed onto
+ :py:func:`argparse.ArgumentParser.add_argument`
+
+ """
for (args, kwargs) in common:
parser.add_argument(*args, **kwargs)
subparsers = parser.add_subparsers()
- for subcmd in sorted(subcommands.keys()):
+ for subcmd in sorted(subcommands):
val = subcommands[subcmd]
- func = val.get('func')
- if not func:
- func = func_namespace.get(
- 'main_' + subcmd.replace("-", "_"), unimplemented_subcommand)
sparser = subparsers.add_parser(subcmd, help=val['help'])
- sparser.set_defaults(func=func, name=subcmd)
+ sparser.set_defaults(func=val['func'], name=subcmd)
for (args, kwargs) in val.get('opts', {}):
sparser.add_argument(*args, **kwargs)
@@ -54,29 +65,36 @@ COMMON_ARGS = [
SUBCOMMANDS = {
# The stages a normal boot takes
'network': {
+ 'func': unimplemented_subcommand,
'help': 'locate and apply networking configuration',
},
'search': {
+ 'func': unimplemented_subcommand,
'help': 'search available data sources',
},
'config': {
+ 'func': unimplemented_subcommand,
'help': 'run available config modules',
},
'config-final': {
+ 'func': unimplemented_subcommand,
'help': 'run "final" config modules',
},
# utility
'version': {
+ 'func': main_version,
'help': 'print cloud-init version',
},
'all': {
+ 'func': unimplemented_subcommand,
+ 'help': 'run all stages as if from boot',
'opts': [
(('--clean',),
{'help': 'clear any prior system state',
'action': 'store_true', 'default': False})],
- 'help': 'run all stages as if from boot',
},
'clean': {
+ 'func': unimplemented_subcommand,
'help': 'clear any prior system state.',
'opts': [
(('-F', '--full'),
@@ -85,6 +103,7 @@ SUBCOMMANDS = {
],
},
'query': {
+ 'func': unimplemented_subcommand,
'help': 'query system state',
'opts': [
(('--json',),
diff --git a/cloudinit/tests/test_shell.py b/cloudinit/tests/test_shell.py
index 3983763..401d782 100644
--- a/cloudinit/tests/test_shell.py
+++ b/cloudinit/tests/test_shell.py
@@ -5,11 +5,11 @@
import cloudinit.shell as shell
-from cloudinit.test import TestCase
+from cloudinit.tests import TestCase
from cloudinit.tests.util import mock
-class TestCase(TestCase):
+class TestMain(TestCase):
@mock.patch('cloudinit.shell.main_version')
def test_main_has_version(self, main_version):
|