Skip to content

Commit d913f06

Browse files
docs: demonstrate mutually_exclusive_groups in example
Adds a do_export command so mutually_exclusive_groups has a runnable demo alongside the existing groups demo.
1 parent e30763e commit d913f06

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

examples/annotated_example.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,25 @@ def do_connect(self, host: str, port: int = 22, verbose: bool = False) -> None:
338338
msg = f"Connecting to {host}:{port}"
339339
self.poutput(f"{msg} (verbose)" if verbose else msg)
340340

341+
# -- Mutually exclusive groups -------------------------------------------
342+
# Group instances passed to mutually_exclusive_groups make argparse reject
343+
# combinations (title/description are ignored here).
344+
345+
@with_annotated(
346+
description="Export data in exactly one format.",
347+
mutually_exclusive_groups=(Group("json", "csv"),),
348+
)
349+
@cmd2.with_category(ANNOTATED_CATEGORY)
350+
def do_export(self, name: str, json: bool = False, csv: bool = False) -> None:
351+
"""Export a dataset; --json and --csv are mutually exclusive.
352+
353+
Try:
354+
export sales --json
355+
export sales --json --csv # rejected: not allowed together
356+
"""
357+
fmt = "json" if json else "csv" if csv else "text"
358+
self.poutput(f"Exporting {name} as {fmt}")
359+
341360
# -- Custom formatter and parser classes ---------------------------------
342361
# A custom help formatter or Cmd2ArgumentParser subclass can be supplied.
343362

0 commit comments

Comments
 (0)