Skip to content

Commit cf9516e

Browse files
committed
Added --silent flag to alias/macro create.
Added --with_silent flag to alias/macro list.
1 parent d25534a commit cf9516e

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
not display hints even when this setting is True.
66
* argparse tab completion now groups flag names which run the same action. Optional flags are wrapped
77
in brackets like it is done in argparse usage text.
8+
* Added `--silent` flag to `alias/macro create`. If used, then no confirmation message will be printed
9+
when aliases and macros are created or overwritten.
10+
* Added `--with_silent` flag to `alias/macro list`. Use this option when saving to a startup script
11+
that should silently create aliases and macros.
812
* Bug Fixes
913
* Fixed issue where flag names weren't always sorted correctly in argparse tab completion
1014

cmd2/cmd2.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2729,6 +2729,9 @@ def do_alias(self, args: argparse.Namespace) -> None:
27292729
" alias create save_results print_results \">\" out.txt\n")
27302730

27312731
alias_create_parser = DEFAULT_ARGUMENT_PARSER(description=alias_create_description, epilog=alias_create_epilog)
2732+
alias_create_parser.add_argument('-s', '--silent', action='store_true',
2733+
help='do not print message confirming alias was created or\n'
2734+
'overwritten')
27322735
alias_create_parser.add_argument('name', help='name of this alias')
27332736
alias_create_parser.add_argument('command', help='what the alias resolves to',
27342737
choices_method=_get_commands_aliases_and_macros_for_completion)
@@ -2738,7 +2741,6 @@ def do_alias(self, args: argparse.Namespace) -> None:
27382741
@as_subcommand_to('alias', 'create', alias_create_parser, help=alias_create_description.lower())
27392742
def _alias_create(self, args: argparse.Namespace) -> None:
27402743
"""Create or overwrite an alias"""
2741-
27422744
# Validate the alias name
27432745
valid, errmsg = self.statement_parser.is_valid_command(args.name)
27442746
if not valid:
@@ -2766,16 +2768,18 @@ def _alias_create(self, args: argparse.Namespace) -> None:
27662768
# Set the alias
27672769
result = "overwritten" if args.name in self.aliases else "created"
27682770
self.aliases[args.name] = value
2769-
self.poutput("Alias '{}' {}".format(args.name, result))
2771+
2772+
if not args.silent:
2773+
self.poutput("Alias '{}' {}".format(args.name, result))
27702774

27712775
# alias -> delete
27722776
alias_delete_help = "delete aliases"
27732777
alias_delete_description = "Delete specified aliases or all aliases if --all is used"
27742778

27752779
alias_delete_parser = DEFAULT_ARGUMENT_PARSER(description=alias_delete_description)
2780+
alias_delete_parser.add_argument('-a', '--all', action='store_true', help="delete all aliases")
27762781
alias_delete_parser.add_argument('names', nargs=argparse.ZERO_OR_MORE, help='alias(es) to delete',
27772782
choices_method=_get_alias_completion_items, descriptive_header='Value')
2778-
alias_delete_parser.add_argument('-a', '--all', action='store_true', help="delete all aliases")
27792783

27802784
@as_subcommand_to('alias', 'delete', alias_delete_parser, help=alias_delete_help)
27812785
def _alias_delete(self, args: argparse.Namespace) -> None:
@@ -2801,21 +2805,29 @@ def _alias_delete(self, args: argparse.Namespace) -> None:
28012805
"Without arguments, all aliases will be listed.")
28022806

28032807
alias_list_parser = DEFAULT_ARGUMENT_PARSER(description=alias_list_description)
2808+
alias_list_parser.add_argument('-w', '--with_silent', action='store_true',
2809+
help="include --silent flag with listed aliases\n"
2810+
"Use this option when saving to a startup script that\n"
2811+
"should silently create aliases.")
28042812
alias_list_parser.add_argument('names', nargs=argparse.ZERO_OR_MORE, help='alias(es) to list',
28052813
choices_method=_get_alias_completion_items, descriptive_header='Value')
28062814

28072815
@as_subcommand_to('alias', 'list', alias_list_parser, help=alias_delete_help)
28082816
def _alias_list(self, args: argparse.Namespace) -> None:
28092817
"""List some or all aliases"""
2818+
create_cmd = "alias create"
2819+
if args.with_silent:
2820+
create_cmd += " --silent"
2821+
28102822
if args.names:
28112823
for cur_name in utils.remove_duplicates(args.names):
28122824
if cur_name in self.aliases:
2813-
self.poutput("alias create {} {}".format(cur_name, self.aliases[cur_name]))
2825+
self.poutput("{} {} {}".format(create_cmd, cur_name, self.aliases[cur_name]))
28142826
else:
28152827
self.perror("Alias '{}' not found".format(cur_name))
28162828
else:
28172829
for cur_alias in sorted(self.aliases, key=self.default_sort_key):
2818-
self.poutput("alias create {} {}".format(cur_alias, self.aliases[cur_alias]))
2830+
self.poutput("{} {} {}".format(create_cmd, cur_alias, self.aliases[cur_alias]))
28192831

28202832
#############################################################
28212833
# Parsers and functions for macro command and subcommands
@@ -2879,6 +2891,9 @@ def do_macro(self, args: argparse.Namespace) -> None:
28792891
" will only complete paths while typing a macro.")
28802892

28812893
macro_create_parser = DEFAULT_ARGUMENT_PARSER(description=macro_create_description, epilog=macro_create_epilog)
2894+
macro_create_parser.add_argument('-s', '--silent', action='store_true',
2895+
help='do not print message confirming macro was created or\n'
2896+
'overwritten')
28822897
macro_create_parser.add_argument('name', help='name of this macro')
28832898
macro_create_parser.add_argument('command', help='what the macro resolves to',
28842899
choices_method=_get_commands_aliases_and_macros_for_completion)
@@ -2888,7 +2903,6 @@ def do_macro(self, args: argparse.Namespace) -> None:
28882903
@as_subcommand_to('macro', 'create', macro_create_parser, help=macro_create_help)
28892904
def _macro_create(self, args: argparse.Namespace) -> None:
28902905
"""Create or overwrite a macro"""
2891-
28922906
# Validate the macro name
28932907
valid, errmsg = self.statement_parser.is_valid_command(args.name)
28942908
if not valid:
@@ -2963,15 +2977,17 @@ def _macro_create(self, args: argparse.Namespace) -> None:
29632977
# Set the macro
29642978
result = "overwritten" if args.name in self.macros else "created"
29652979
self.macros[args.name] = Macro(name=args.name, value=value, minimum_arg_count=max_arg_num, arg_list=arg_list)
2966-
self.poutput("Macro '{}' {}".format(args.name, result))
2980+
2981+
if not args.silent:
2982+
self.poutput("Macro '{}' {}".format(args.name, result))
29672983

29682984
# macro -> delete
29692985
macro_delete_help = "delete macros"
29702986
macro_delete_description = "Delete specified macros or all macros if --all is used"
29712987
macro_delete_parser = DEFAULT_ARGUMENT_PARSER(description=macro_delete_description)
2988+
macro_delete_parser.add_argument('-a', '--all', action='store_true', help="delete all macros")
29722989
macro_delete_parser.add_argument('names', nargs=argparse.ZERO_OR_MORE, help='macro(s) to delete',
29732990
choices_method=_get_macro_completion_items, descriptive_header='Value')
2974-
macro_delete_parser.add_argument('-a', '--all', action='store_true', help="delete all macros")
29752991

29762992
@as_subcommand_to('macro', 'delete', macro_delete_parser, help=macro_delete_help)
29772993
def _macro_delete(self, args: argparse.Namespace) -> None:
@@ -2997,21 +3013,29 @@ def _macro_delete(self, args: argparse.Namespace) -> None:
29973013
"Without arguments, all macros will be listed.")
29983014

29993015
macro_list_parser = DEFAULT_ARGUMENT_PARSER(description=macro_list_description)
3016+
macro_list_parser.add_argument('-w', '--with_silent', action='store_true',
3017+
help="include --silent flag with listed macros\n"
3018+
"Use this option when saving to a startup script that\n"
3019+
"should silently create macros.")
30003020
macro_list_parser.add_argument('names', nargs=argparse.ZERO_OR_MORE, help='macro(s) to list',
30013021
choices_method=_get_macro_completion_items, descriptive_header='Value')
30023022

30033023
@as_subcommand_to('macro', 'list', macro_list_parser, help=macro_list_help)
30043024
def _macro_list(self, args: argparse.Namespace) -> None:
30053025
"""List some or all macros"""
3026+
create_cmd = "macro create"
3027+
if args.with_silent:
3028+
create_cmd += " --silent"
3029+
30063030
if args.names:
30073031
for cur_name in utils.remove_duplicates(args.names):
30083032
if cur_name in self.macros:
3009-
self.poutput("macro create {} {}".format(cur_name, self.macros[cur_name].value))
3033+
self.poutput("{} {} {}".format(create_cmd, cur_name, self.macros[cur_name].value))
30103034
else:
30113035
self.perror("Macro '{}' not found".format(cur_name))
30123036
else:
30133037
for cur_macro in sorted(self.macros, key=self.default_sort_key):
3014-
self.poutput("macro create {} {}".format(cur_macro, self.macros[cur_macro].value))
3038+
self.poutput("{} {} {}".format(create_cmd, cur_macro, self.macros[cur_macro].value))
30153039

30163040
def complete_help_command(self, text: str, line: str, begidx: int, endidx: int) -> List[str]:
30173041
"""Completes the command argument of help"""
@@ -3046,12 +3070,12 @@ def complete_help_subcommands(self, text: str, line: str, begidx: int, endidx: i
30463070

30473071
help_parser = DEFAULT_ARGUMENT_PARSER(description="List available commands or provide "
30483072
"detailed help for a specific command")
3073+
help_parser.add_argument('-v', '--verbose', action='store_true',
3074+
help="print a list of all commands with descriptions of each")
30493075
help_parser.add_argument('command', nargs=argparse.OPTIONAL, help="command to retrieve help for",
30503076
completer_method=complete_help_command)
30513077
help_parser.add_argument('subcommands', nargs=argparse.REMAINDER, help="subcommand(s) to retrieve help for",
30523078
completer_method=complete_help_subcommands)
3053-
help_parser.add_argument('-v', '--verbose', action='store_true',
3054-
help="print a list of all commands with descriptions of each")
30553079

30563080
# Get rid of cmd's complete_help() functions so ArgparseCompleter will complete the help command
30573081
if getattr(cmd.Cmd, 'complete_help', None) is not None:

tests/test_cmd2.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,21 @@ def test_alias_create(base_app):
16241624
out, err = run_cmd(base_app, 'alias list fake')
16251625
assert out == normalize('alias create fake run_pyscript')
16261626

1627+
# Overwrite alias
1628+
out, err = run_cmd(base_app, 'alias create fake help')
1629+
assert out == normalize("Alias 'fake' overwritten")
1630+
1631+
# Look up the updated alias
1632+
out, err = run_cmd(base_app, 'alias list fake')
1633+
assert out == normalize('alias create fake help')
1634+
1635+
# Test silent flag
1636+
out, err = run_cmd(base_app, 'alias create --silent fake set')
1637+
assert not out
1638+
1639+
out, err = run_cmd(base_app, 'alias list --with_silent fake')
1640+
assert out == normalize('alias create --silent fake set')
1641+
16271642
def test_alias_create_with_quoted_value(base_app):
16281643
"""Demonstrate that quotes in alias value will be preserved (except for redirectors and terminators)"""
16291644

@@ -1718,6 +1733,21 @@ def test_macro_create(base_app):
17181733
out, err = run_cmd(base_app, 'macro list fake')
17191734
assert out == normalize('macro create fake run_pyscript')
17201735

1736+
# Overwrite macro
1737+
out, err = run_cmd(base_app, 'macro create fake help')
1738+
assert out == normalize("Macro 'fake' overwritten")
1739+
1740+
# Look up the updated macro
1741+
out, err = run_cmd(base_app, 'macro list fake')
1742+
assert out == normalize('macro create fake help')
1743+
1744+
# Test silent flag
1745+
out, err = run_cmd(base_app, 'macro create --silent fake set')
1746+
assert not out
1747+
1748+
out, err = run_cmd(base_app, 'macro list --with_silent fake')
1749+
assert out == normalize('macro create --silent fake set')
1750+
17211751
def test_macro_create_with_quoted_value(base_app):
17221752
"""Demonstrate that quotes in macro value will be preserved (except for redirectors and terminators)"""
17231753
# Create the macro

0 commit comments

Comments
 (0)