| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
""" User registers commands with CommandGroups """ |
| 4 |
|
| 5 |
import os |
| 6 |
import sys |
| 7 |
from collections import OrderedDict |
| 8 |
|
| 9 |
from knack import CLI |
| 10 |
from knack.commands import CLICommandsLoader, CommandGroup |
| 11 |
from knack.arguments import ArgumentsContext |
| 12 |
from knack.help import CLIHelp |
| 13 |
|
| 14 |
from knack.help_files import helps |
| 15 |
|
| 16 |
cli_name = os.path.basename(__file__) |
| 17 |
|
| 18 |
helps['abc'] = """ |
| 19 |
type: group |
| 20 |
short-summary: Manage the alphabet of words. |
| 21 |
""" |
| 22 |
|
| 23 |
helps['abc list'] = """ |
| 24 |
type: command |
| 25 |
short-summary: List the alphabet. |
| 26 |
examples: |
| 27 |
- name: It's pretty straightforward. |
| 28 |
text: {cli_name} abc list |
| 29 |
""".format(cli_name=cli_name) |
| 30 |
|
| 31 |
helps['abc first'] = """ |
| 32 |
type: command |
| 33 |
short-summary: List the first several letters in the alphabet. |
| 34 |
examples: |
| 35 |
- name: Show the list of abc |
| 36 |
text: {cli_name} abc first --number 3 |
| 37 |
""".format(cli_name=cli_name) |
| 38 |
|
| 39 |
helps['abc last'] = """ |
| 40 |
type: command |
| 41 |
short-summary: List the last several letters in the alphabet. |
| 42 |
examples: |
| 43 |
- name: Show the list of xyz |
| 44 |
text: {cli_name} abc last --number 3 |
| 45 |
""".format(cli_name=cli_name) |
| 46 |
|
| 47 |
helps['ga'] = """ |
| 48 |
type: group |
| 49 |
short-summary: A general available command group |
| 50 |
""" |
| 51 |
|
| 52 |
helps['pre'] = """ |
| 53 |
type: group |
| 54 |
short-summary: A preview command group |
| 55 |
""" |
| 56 |
|
| 57 |
helps['exp'] = """ |
| 58 |
type: group |
| 59 |
short-summary: An experimental command group |
| 60 |
""" |
| 61 |
|
| 62 |
helps['demo'] = """ |
| 63 |
type: group |
| 64 |
short-summary: A command group for demos. |
| 65 |
""" |
| 66 |
|
| 67 |
helps['demo arg'] = """ |
| 68 |
type: group |
| 69 |
short-summary: A command showing how to use arguments. |
| 70 |
""" |
| 71 |
|
| 72 |
|
| 73 |
def abc_show_command_handler(): |
| 74 |
""" |
| 75 |
Show a JSON mapping of letters to their ASCII values |
| 76 |
""" |
| 77 |
import string |
| 78 |
lower = {} |
| 79 |
for ch in string.ascii_lowercase: |
| 80 |
lower[ch] = ord(ch) |
| 81 |
upper = {} |
| 82 |
for ch in string.ascii_uppercase: |
| 83 |
upper[ch] = ord(ch) |
| 84 |
return {"lowercase": lower, "uppercase": upper} |
| 85 |
|
| 86 |
|
| 87 |
def abc_list_command_handler(): |
| 88 |
import string |
| 89 |
return list(string.ascii_lowercase) |
| 90 |
|
| 91 |
|
| 92 |
def abc_first_command_handler(number=5): |
| 93 |
import string |
| 94 |
return list(string.ascii_lowercase)[0:number] |
| 95 |
|
| 96 |
|
| 97 |
def abc_last_command_handler(number=5): |
| 98 |
import string |
| 99 |
return list(string.ascii_lowercase)[-number:] |
| 100 |
|
| 101 |
|
| 102 |
def range_command_handler(start=0, end=5): |
| 103 |
""" |
| 104 |
Get a list of natural numbers from start to end |
| 105 |
:param start: the lower bound |
| 106 |
:param end: the higher bound |
| 107 |
:return: |
| 108 |
""" |
| 109 |
return list(range(int(start), int(end) + 1)) |
| 110 |
|
| 111 |
|
| 112 |
def sample_json_handler(): |
| 113 |
""" |
| 114 |
Get a sample JSON string |
| 115 |
""" |
| 116 |
# https://docs.microsoft.com/en-us/rest/api/resources/subscriptions/list#examples |
| 117 |
result = { |
| 118 |
"id": "/subscriptions/291bba3f-e0a5-47bc-a099-3bdcb2a50a05", |
| 119 |
"subscriptionId": "291bba3f-e0a5-47bc-a099-3bdcb2a50a05", |
| 120 |
"tenantId": "31c75423-32d6-4322-88b7-c478bdde4858", |
| 121 |
"displayName": "Example Subscription", |
| 122 |
"state": "Enabled", |
| 123 |
"subscriptionPolicies": { |
| 124 |
"locationPlacementId": "Internal_2014-09-01", |
| 125 |
"quotaId": "Internal_2014-09-01", |
| 126 |
"spendingLimit": "Off" |
| 127 |
}, |
| 128 |
"authorizationSource": "RoleBased", |
| 129 |
"managedByTenants": [ |
| 130 |
{ |
| 131 |
"tenantId": "8f70baf1-1f6e-46a2-a1ff-238dac1ebfb7" |
| 132 |
} |
| 133 |
] |
| 134 |
} |
| 135 |
return result |
| 136 |
|
| 137 |
|
| 138 |
def sample_logger_handler(): |
| 139 |
""" |
| 140 |
Print logs to stderr. |
| 141 |
""" |
| 142 |
print("""This is a demo for logging. The logging level can be controlled with: |
| 143 |
--only-show-errors: Show ERROR logs and above |
| 144 |
<default>: Show WARNING logs and above |
| 145 |
--verbose: Show INFO logs and above |
| 146 |
--debug: Show DEBUG logs and above""") |
| 147 |
from knack.log import get_logger |
| 148 |
logger = get_logger(__name__) |
| 149 |
logger.debug("This is a debug log entry.") |
| 150 |
logger.info("This is a info log entry.") |
| 151 |
logger.warning("This is a warning log entry.") |
| 152 |
logger.error("This is a error log entry.") |
| 153 |
logger.critical("This is a critical log entry.") |
| 154 |
|
| 155 |
|
| 156 |
def hello_command_handler(greetings=None): |
| 157 |
""" |
| 158 |
Say "Hello World!" and my warm greetings |
| 159 |
:param greetings: My warm greetings |
| 160 |
""" |
| 161 |
return ['Hello World!', greetings] |
| 162 |
|
| 163 |
|
| 164 |
def demo_arg_handler(move=None): |
| 165 |
if move: |
| 166 |
print("Your move was: {}".format(move)) |
| 167 |
return |
| 168 |
print("Nothing to do.") |
| 169 |
|
| 170 |
|
| 171 |
WELCOME_MESSAGE = r""" |
| 172 |
_____ _ _____ |
| 173 |
/ ____| | |_ _| |
| 174 |
| | | | | | |
| 175 |
| | | | | | |
| 176 |
| |____| |____ _| |_ |
| 177 |
\_____|______|_____| |
| 178 |
|
| 179 |
|
| 180 |
Welcome to the cool new CLI! |
| 181 |
""" |
| 182 |
|
| 183 |
|
| 184 |
class MyCLIHelp(CLIHelp): |
| 185 |
|
| 186 |
def __init__(self, cli_ctx=None): |
| 187 |
super(MyCLIHelp, self).__init__(cli_ctx=cli_ctx, |
| 188 |
privacy_statement='My privacy statement.', |
| 189 |
welcome_message=WELCOME_MESSAGE) |
| 190 |
|
| 191 |
|
| 192 |
class MyCommandsLoader(CLICommandsLoader): |
| 193 |
|
| 194 |
def load_command_table(self, args): |
| 195 |
with CommandGroup(self, '', '__main__#{}') as g: |
| 196 |
g.command('hello', 'hello_command_handler', confirmation=True) |
| 197 |
g.command('sample-json', 'sample_json_handler') |
| 198 |
g.command('sample-logger', 'sample_logger_handler') |
| 199 |
|
| 200 |
with CommandGroup(self, 'abc', '__main__#{}') as g: |
| 201 |
g.command('list', 'abc_list_command_handler') |
| 202 |
g.command('show', 'abc_show_command_handler') |
| 203 |
g.command('get', 'abc_show_command_handler', deprecate_info=g.deprecate(redirect='show', hide='1.0.0')) |
| 204 |
g.command('first', 'abc_first_command_handler', is_preview=True) |
| 205 |
g.command('last', 'abc_last_command_handler', is_experimental=True) |
| 206 |
|
| 207 |
# A GA command group |
| 208 |
with CommandGroup(self, 'ga', '__main__#{}') as g: |
| 209 |
g.command('range', 'range_command_handler') |
| 210 |
|
| 211 |
# A preview command group |
| 212 |
with CommandGroup(self, 'pre', '__main__#{}', is_preview=True) as g: |
| 213 |
g.command('first', 'abc_first_command_handler', is_preview=True) |
| 214 |
g.command('range', 'range_command_handler') |
| 215 |
|
| 216 |
# An experimental command group |
| 217 |
with CommandGroup(self, 'exp', '__main__#{}', is_experimental=True) as g: |
| 218 |
g.command('range', 'range_command_handler') |
| 219 |
|
| 220 |
# A deprecated command group |
| 221 |
with CommandGroup(self, 'dep', '__main__#{}', deprecate_info=g.deprecate(redirect='ga', hide='1.0.0')) as g: |
| 222 |
g.command('range', 'range_command_handler') |
| 223 |
|
| 224 |
with CommandGroup(self, 'demo', '__main__#{}') as g: |
| 225 |
g.command('arg', 'demo_arg_handler') |
| 226 |
|
| 227 |
return super(MyCommandsLoader, self).load_command_table(args) |
| 228 |
|
| 229 |
def load_arguments(self, command): |
| 230 |
with ArgumentsContext(self, 'ga range') as ac: |
| 231 |
ac.argument('start', type=int, is_preview=True) |
| 232 |
ac.argument('end', type=int, is_experimental=True) |
| 233 |
|
| 234 |
with ArgumentsContext(self, 'demo arg') as ac: |
| 235 |
ac.argument('move', choices=['rock', 'paper', 'scissors']) |
| 236 |
|
| 237 |
super(MyCommandsLoader, self).load_arguments(command) |
| 238 |
|
| 239 |
|
| 240 |
class MyCLI(CLI): |
| 241 |
|
| 242 |
def get_cli_version(self): |
| 243 |
return '0.1.0' |
| 244 |
|
| 245 |
|
| 246 |
mycli = MyCLI(cli_name=cli_name, |
| 247 |
config_dir=os.path.expanduser(os.path.join('~', '.{}'.format(cli_name))), |
| 248 |
config_env_var_prefix=cli_name, |
| 249 |
commands_loader_cls=MyCommandsLoader, |
| 250 |
help_cls=MyCLIHelp) |
| 251 |
exit_code = mycli.invoke(sys.argv[1:]) |
| 252 |
sys.exit(exit_code) |