MCPcopy Create free account
hub / github.com/Textualize/trogon / _to_cli_args

Method _to_cli_args

trogon/run_command.py:93–219  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

91 return cli_args
92
93 def _to_cli_args(self) -> list[str]:
94 args: list[str] = [self.name]
95
96 multiples: dict[str, list[tuple[str]]] = defaultdict(list)
97 multiples_schemas: dict[str, OptionSchema] = {}
98
99 for option in self.options:
100 if option.option_schema.multiple:
101 # We need to gather the items for the same option,
102 # compare them to the default, then display them all
103 # if they aren't equivalent to the default.
104 multiples[option.string_name].append(option.value)
105 multiples_schemas[option.string_name] = option.option_schema
106 else:
107 value_data: list[tuple[Any]] = MultiValueParamData.process_cli_option(
108 option.value
109 ).values
110
111 if option.option_schema.default is not None:
112 default_data: list[tuple[Any]] = option.option_schema.default.values
113 else:
114 default_data = [tuple()]
115
116 flattened_values = sorted(itertools.chain.from_iterable(value_data))
117 flattened_defaults = sorted(itertools.chain.from_iterable(default_data))
118
119 # If the user has supplied values (any values are not None), then
120 # we don't display the value.
121 values_supplied = any(
122 value != ValueNotSupplied() for value in flattened_values
123 )
124 values_are_defaults = list(map(str, flattened_values)) == list(
125 map(str, flattened_defaults)
126 )
127
128 # If the user has supplied values, and they're not the default values,
129 # then we want to display them in the command string...
130 if values_supplied and not values_are_defaults:
131 if isinstance(option.name, str):
132 option_name = option.name
133 else:
134 if option.option_schema.counting:
135 # For count options, we use the shortest name, e.g. use
136 # -v instead of --verbose.
137 option_name = min(option.name, key=len)
138 else:
139 # Use the option with the longest name, since
140 # it's probably the most descriptive (use --verbose over -v)
141 option_name = max(option.name, key=len)
142
143 is_true_bool = value_data == [(True,)]
144
145 is_flag = option.option_schema.is_flag
146 secondary_opts = option.option_schema.secondary_opts
147
148 if is_flag:
149 # If the option is specified like `--thing/--not-thing`,
150 # then secondary_opts will contain `--not-thing`, and if the

Callers 1

to_cli_argsMethod · 0.95

Calls 2

ValueNotSuppliedClass · 0.90
process_cli_optionMethod · 0.80

Tested by

no test coverage detected