def get_three_state_flag(positive_label='true', negative_label='false', invert=False, return_label=False): """ Creates a flag-like argument that can also accept positive/negative values. This allows consistency between create commands that typically use flags and update commands that require positive/negative values without introducing breaking changes. Flag-like behavior always implies the affirmative unless invert=True then invert the logic. - positive_label: label for the positive value (ex: 'enabled') - negative_label: label for the negative value (ex: 'disabled') - invert: invert the boolean logic for the flag - return_label: if true, return the corresponding label. Otherwise, return a boolean value """ choices = [positive_label, negative_label] # pylint: disable=too-few-public-methods class ThreeStateAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): values = values or positive_label is_positive = values.lower() == positive_label.lower() is_positive = not is_positive if invert else is_positive set_val = None if return_label: set_val = positive_label if is_positive else negative_label else: set_val = is_positive setattr(namespace, self.dest, set_val) params = { 'choices': CaseInsensitiveList(choices), 'nargs': '?', 'action': ThreeStateAction } return CLIArgumentType(**params)
def get_enum_type(data, default=None): """ Creates the argparse choices and type kwargs for a supplied enum type or list of strings. """ if not data: return None # transform enum types, otherwise assume list of string choices try: choices = [x.value for x in data] except AttributeError: choices = data # pylint: disable=too-few-public-methods class DefaultAction(argparse.Action): def __call__(self, parser, args, values, option_string=None): def _get_value(val): return next( (x for x in self.choices if x.lower() == val.lower()), val) if isinstance(values, list): values = [_get_value(v) for v in values] else: values = _get_value(values) setattr(args, self.dest, values) def _type(value): return next((x for x in choices if x.lower() == value.lower()), value) if value else value default_value = None if default: default_value = next( (x for x in choices if x.lower() == default.lower()), None) if not default_value: raise CLIError( "Command authoring exception: urecognized default '{}' from choices '{}'" .format(default, choices)) arg_type = CLIArgumentType(choices=CaseInsensitiveList(choices), action=DefaultAction, default=default_value) else: arg_type = CLIArgumentType(choices=CaseInsensitiveList(choices), action=DefaultAction) return arg_type
# -------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- """ CLI parameter definitions. """ from knack.arguments import CLIArgumentType, CaseInsensitiveList from azure.cli.core.commands.parameters import get_three_state_flag from azext_iot.monitor.models.enum import Severity from azext_iot._params import event_msg_prop_type, event_timeout_type severity_type = CLIArgumentType( options_list=["--minimum-severity"], choices=CaseInsensitiveList([sev.name for sev in Severity]), help="Minimum severity of issue required for reporting.", ) style_type = CLIArgumentType( options_list=["--style"], choices=CaseInsensitiveList(["scroll", "json", "csv"]), help="Indicate output style" "scroll = deliver errors as they arrive, json = summarize results as json, csv = summarize results as json", ) def load_central_arguments(self, _): """ Load CLI Args for Knack parser """
JobStatusType, AuthenticationType, RenewKeyType, ) from azext_iot._validators import mode2_iot_login_handler from azext_iot.assets.user_messages import info_param_properties_device hub_name_type = CLIArgumentType( completer=get_resource_name_completion_list("Microsoft.Devices/IotHubs"), help="IoT Hub name.", ) event_msg_prop_type = CLIArgumentType( options_list=["--properties", "--props", "-p"], nargs="*", choices=CaseInsensitiveList(["sys", "app", "anno", "all"]), help="Indicate key message properties to output. " "sys = system properties, app = application properties, anno = annotations", ) # There is a bug in CLI core preventing treating --qos as an integer. # Until its resolved, ensure casting of value to integer # TODO: azure.cli.core.parser line 180 difflib.get_close_matches qos_type = CLIArgumentType( options_list=["--qos"], type=str, nargs="?", choices=["0", "1"], help="Quality of Service. 0 = At most once, 1 = At least once. 2 (Exactly once) is not supported.", )
# -------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- # pylint: disable=line-too-long import os.path from argcomplete.completers import FilesCompleter from azure.cli.core.commands.parameters import get_location_type, get_enum_type, file_type, tags_type from azure.cli.core.commands.validators import get_default_location_from_resource_group from azext_connectedk8s._constants import Distribution_Enum_Values, Infrastructure_Enum_Values, Feature_Values from knack.arguments import (CLIArgumentType, CaseInsensitiveList) features_types = CLIArgumentType(nargs='+', choices=CaseInsensitiveList(Feature_Values)) def load_arguments(self, _): with self.argument_context('connectedk8s connect') as c: c.argument('tags', tags_type) c.argument('location', arg_type=get_location_type(self.cli_ctx), validator=get_default_location_from_resource_group) c.argument('cluster_name', options_list=['--name', '-n'], help='The name of the connected cluster.') c.argument('kube_config', options_list=['--kube-config'], help='Path to the kube config file.') c.argument('kube_context',
get_three_state_flag) from azext_iot.common.shared import (EntityStatusType, SettleType, DeviceAuthType, KeyType, AttestationType, ProtocolType, AckType, MetricType, ReprovisionType, AllocationType, DistributedTracingSamplingModeType, ModelSourceType) from azext_iot._validators import mode2_iot_login_handler hub_name_type = CLIArgumentType( completer=get_resource_name_completion_list('Microsoft.Devices/IotHubs'), help='IoT Hub name.') event_msg_prop_type = CLIArgumentType( nargs='*', choices=CaseInsensitiveList(['sys', 'app', 'anno', 'all']), help='Indicate key message properties to output. ' 'sys = system properties, app = application properties, anno = annotations' ) # pylint: disable=too-many-statements def load_arguments(self, _): """ Load CLI Args for Knack parser """ with self.argument_context('iot') as context: context.argument( 'login', options_list=['--login', '-l'], validator=mode2_iot_login_handler,
# Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- """ CLI parameter definitions. """ from knack.arguments import CLIArgumentType, CaseInsensitiveList from azure.cli.core.commands.parameters import get_three_state_flag from azext_iot.monitor.models.enum import Severity from azext_iot.central.models.enum import Role, ApiVersion from azext_iot._params import event_msg_prop_type, event_timeout_type severity_type = CLIArgumentType( options_list=["--minimum-severity"], choices=CaseInsensitiveList([sev.name for sev in Severity]), help="Minimum severity of issue required for reporting.", ) role_type = CLIArgumentType( options_list=["--role", "-r"], choices=CaseInsensitiveList([role.name for role in Role]), help="The role that will be associated with this token." " You can specify one of the built-in roles, or specify the role ID of a custom role." " See more at https://aka.ms/iotcentral-customrolesdocs", ) style_type = CLIArgumentType( options_list=["--style"], choices=CaseInsensitiveList(["scroll", "json", "csv"]), help="Indicate output style"
def to_choices(self): """Generate choices property of CLICommandArgument""" choices = list(self.items) if not self._case_sensitive: choices = CaseInsensitiveList(choices) return choices