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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
#!/usr/bin/env python
""" User registers commands with CommandGroups """
import os
import sys
from collections import OrderedDict
from knack import CLI
from knack.commands import CLICommandsLoader, CommandGroup
from knack.arguments import ArgumentsContext
from knack.help import CLIHelp
from knack.help_files import helps
cli_name = os.path.basename(__file__)
helps['abc'] = """
type: group
short-summary: Manage the alphabet of words.
"""
helps['abc list'] = """
type: command
short-summary: List the alphabet.
examples:
- name: It's pretty straightforward.
text: {cli_name} abc list
""".format(cli_name=cli_name)
helps['abc first'] = """
type: command
short-summary: List the first several letters in the alphabet.
examples:
- name: Show the list of abc
text: {cli_name} abc first --number 3
""".format(cli_name=cli_name)
helps['abc last'] = """
type: command
short-summary: List the last several letters in the alphabet.
examples:
- name: Show the list of xyz
text: {cli_name} abc last --number 3
""".format(cli_name=cli_name)
helps['ga'] = """
type: group
short-summary: A general available command group
"""
helps['pre'] = """
type: group
short-summary: A preview command group
"""
helps['exp'] = """
type: group
short-summary: An experimental command group
"""
def abc_show_command_handler():
"""
Show a JSON mapping of letters to their ASCII values
"""
import string
lower = {}
for ch in string.ascii_lowercase:
lower[ch] = ord(ch)
upper = {}
for ch in string.ascii_uppercase:
upper[ch] = ord(ch)
return {"lowercase": lower, "uppercase": upper}
def abc_list_command_handler():
import string
return list(string.ascii_lowercase)
def abc_first_command_handler(number=5):
import string
return list(string.ascii_lowercase)[0:number]
def abc_last_command_handler(number=5):
import string
return list(string.ascii_lowercase)[-number:]
def range_command_handler(start=0, end=5):
"""
Get a list of natural numbers from start to end
:param start: the lower bound
:param end: the higher bound
:return:
"""
return list(range(int(start), int(end) + 1))
def sample_json_handler():
"""
Get a sample JSON string
"""
# https://docs.microsoft.com/en-us/rest/api/resources/subscriptions/list#examples
result = {
"id": "/subscriptions/291bba3f-e0a5-47bc-a099-3bdcb2a50a05",
"subscriptionId": "291bba3f-e0a5-47bc-a099-3bdcb2a50a05",
"tenantId": "31c75423-32d6-4322-88b7-c478bdde4858",
"displayName": "Example Subscription",
"state": "Enabled",
"subscriptionPolicies": {
"locationPlacementId": "Internal_2014-09-01",
"quotaId": "Internal_2014-09-01",
"spendingLimit": "Off"
},
"authorizationSource": "RoleBased",
"managedByTenants": [
{
"tenantId": "8f70baf1-1f6e-46a2-a1ff-238dac1ebfb7"
}
]
}
return result
def hello_command_handler(greetings=None):
"""
Say "Hello World!" and my warm greetings
:param greetings: My warm greetings
"""
return ['Hello World!', greetings]
WELCOME_MESSAGE = r"""
_____ _ _____
/ ____| | |_ _|
| | | | | |
| | | | | |
| |____| |____ _| |_
\_____|______|_____|
Welcome to the cool new CLI!
"""
class MyCLIHelp(CLIHelp):
def __init__(self, cli_ctx=None):
super(MyCLIHelp, self).__init__(cli_ctx=cli_ctx,
privacy_statement='My privacy statement.',
welcome_message=WELCOME_MESSAGE)
class MyCommandsLoader(CLICommandsLoader):
def load_command_table(self, args):
with CommandGroup(self, '', '__main__#{}') as g:
g.command('hello', 'hello_command_handler', confirmation=True)
g.command('sample-json', 'sample_json_handler')
with CommandGroup(self, 'abc', '__main__#{}') as g:
g.command('list', 'abc_list_command_handler')
g.command('show', 'abc_show_command_handler')
g.command('get', 'abc_show_command_handler', deprecate_info=g.deprecate(redirect='show', hide='1.0.0'))
g.command('first', 'abc_first_command_handler', is_preview=True)
g.command('last', 'abc_last_command_handler', is_experimental=True)
# A GA command group
with CommandGroup(self, 'ga', '__main__#{}') as g:
g.command('range', 'range_command_handler')
# A preview command group
with CommandGroup(self, 'pre', '__main__#{}', is_preview=True) as g:
g.command('first', 'abc_first_command_handler', is_preview=True)
g.command('range', 'range_command_handler')
# An experimental command group
with CommandGroup(self, 'exp', '__main__#{}', is_experimental=True) as g:
g.command('range', 'range_command_handler')
# A deprecated command group
with CommandGroup(self, 'dep', '__main__#{}', deprecate_info=g.deprecate(redirect='ga', hide='1.0.0')) as g:
g.command('range', 'range_command_handler')
return super(MyCommandsLoader, self).load_command_table(args)
def load_arguments(self, command):
with ArgumentsContext(self, 'ga range') as ac:
ac.argument('start', type=int, is_preview=True)
ac.argument('end', type=int, is_experimental=True)
super(MyCommandsLoader, self).load_arguments(command)
class MyCLI(CLI):
def get_cli_version(self):
return '0.1.0'
mycli = MyCLI(cli_name=cli_name,
config_dir=os.path.expanduser(os.path.join('~', '.{}'.format(cli_name))),
config_env_var_prefix=cli_name,
commands_loader_cls=MyCommandsLoader,
help_cls=MyCLIHelp)
exit_code = mycli.invoke(sys.argv[1:])
sys.exit(exit_code)
|