Skip to content

Commit d348f09

Browse files
authored
Merge pull request #999 from python-cmd2/silence
Added way to silence alias/macro creation
2 parents 92b8a38 + 7d58368 commit d348f09

File tree

3 files changed

+73
-15
lines changed

3 files changed

+73
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 1.4.0 (TBD, 2020)
1+
## 1.3.10 (September 17, 2020)
22
* Enhancements
33
* Added user-settable option called `always_show_hint`. If True, then tab completion hints will always
44
display even when tab completion suggestions print. Arguments whose help or hint text is suppressed will
@@ -7,6 +7,10 @@
77
in brackets like it is done in argparse usage text.
88
* default category decorators are now heritable by default and will propagate the category down the
99
class hierarchy until overridden. There's a new optional flag to set heritable to false.
10+
* Added `--silent` flag to `alias/macro create`. If used, then no confirmation message will be printed
11+
when aliases and macros are created or overwritten.
12+
* Added `--with_silent` flag to `alias/macro list`. Use this option when saving to a startup script
13+
that should silently create aliases and macros.
1014
* Bug Fixes
1115
* Fixed issue where flag names weren't always sorted correctly in argparse tab completion
1216

cmd2/cmd2.py

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,6 +2734,9 @@ def do_alias(self, args: argparse.Namespace) -> None:
27342734
" alias create save_results print_results \">\" out.txt\n")
27352735

27362736
alias_create_parser = DEFAULT_ARGUMENT_PARSER(description=alias_create_description, epilog=alias_create_epilog)
2737+
alias_create_parser.add_argument('-s', '--silent', action='store_true',
2738+
help='do not print message confirming alias was created or\n'
2739+
'overwritten')
27372740
alias_create_parser.add_argument('name', help='name of this alias')
27382741
alias_create_parser.add_argument('command', help='what the alias resolves to',
27392742
choices_method=_get_commands_aliases_and_macros_for_completion)
@@ -2743,7 +2746,6 @@ def do_alias(self, args: argparse.Namespace) -> None:
27432746
@as_subcommand_to('alias', 'create', alias_create_parser, help=alias_create_description.lower())
27442747
def _alias_create(self, args: argparse.Namespace) -> None:
27452748
"""Create or overwrite an alias"""
2746-
27472749
# Validate the alias name
27482750
valid, errmsg = self.statement_parser.is_valid_command(args.name)
27492751
if not valid:
@@ -2769,18 +2771,20 @@ def _alias_create(self, args: argparse.Namespace) -> None:
27692771
value += ' ' + ' '.join(args.command_args)
27702772

27712773
# Set the alias
2772-
result = "overwritten" if args.name in self.aliases else "created"
2774+
if not args.silent:
2775+
result = "overwritten" if args.name in self.aliases else "created"
2776+
self.poutput("Alias '{}' {}".format(args.name, result))
2777+
27732778
self.aliases[args.name] = value
2774-
self.poutput("Alias '{}' {}".format(args.name, result))
27752779

27762780
# alias -> delete
27772781
alias_delete_help = "delete aliases"
27782782
alias_delete_description = "Delete specified aliases or all aliases if --all is used"
27792783

27802784
alias_delete_parser = DEFAULT_ARGUMENT_PARSER(description=alias_delete_description)
2785+
alias_delete_parser.add_argument('-a', '--all', action='store_true', help="delete all aliases")
27812786
alias_delete_parser.add_argument('names', nargs=argparse.ZERO_OR_MORE, help='alias(es) to delete',
27822787
choices_method=_get_alias_completion_items, descriptive_header='Value')
2783-
alias_delete_parser.add_argument('-a', '--all', action='store_true', help="delete all aliases")
27842788

27852789
@as_subcommand_to('alias', 'delete', alias_delete_parser, help=alias_delete_help)
27862790
def _alias_delete(self, args: argparse.Namespace) -> None:
@@ -2806,21 +2810,29 @@ def _alias_delete(self, args: argparse.Namespace) -> None:
28062810
"Without arguments, all aliases will be listed.")
28072811

28082812
alias_list_parser = DEFAULT_ARGUMENT_PARSER(description=alias_list_description)
2813+
alias_list_parser.add_argument('-w', '--with_silent', action='store_true',
2814+
help="include --silent flag with listed aliases\n"
2815+
"Use this option when saving to a startup script that\n"
2816+
"should silently create aliases.")
28092817
alias_list_parser.add_argument('names', nargs=argparse.ZERO_OR_MORE, help='alias(es) to list',
28102818
choices_method=_get_alias_completion_items, descriptive_header='Value')
28112819

28122820
@as_subcommand_to('alias', 'list', alias_list_parser, help=alias_delete_help)
28132821
def _alias_list(self, args: argparse.Namespace) -> None:
28142822
"""List some or all aliases"""
2823+
create_cmd = "alias create"
2824+
if args.with_silent:
2825+
create_cmd += " --silent"
2826+
28152827
if args.names:
28162828
for cur_name in utils.remove_duplicates(args.names):
28172829
if cur_name in self.aliases:
2818-
self.poutput("alias create {} {}".format(cur_name, self.aliases[cur_name]))
2830+
self.poutput("{} {} {}".format(create_cmd, cur_name, self.aliases[cur_name]))
28192831
else:
28202832
self.perror("Alias '{}' not found".format(cur_name))
28212833
else:
28222834
for cur_alias in sorted(self.aliases, key=self.default_sort_key):
2823-
self.poutput("alias create {} {}".format(cur_alias, self.aliases[cur_alias]))
2835+
self.poutput("{} {} {}".format(create_cmd, cur_alias, self.aliases[cur_alias]))
28242836

28252837
#############################################################
28262838
# Parsers and functions for macro command and subcommands
@@ -2884,6 +2896,9 @@ def do_macro(self, args: argparse.Namespace) -> None:
28842896
" will only complete paths while typing a macro.")
28852897

28862898
macro_create_parser = DEFAULT_ARGUMENT_PARSER(description=macro_create_description, epilog=macro_create_epilog)
2899+
macro_create_parser.add_argument('-s', '--silent', action='store_true',
2900+
help='do not print message confirming macro was created or\n'
2901+
'overwritten')
28872902
macro_create_parser.add_argument('name', help='name of this macro')
28882903
macro_create_parser.add_argument('command', help='what the macro resolves to',
28892904
choices_method=_get_commands_aliases_and_macros_for_completion)
@@ -2893,7 +2908,6 @@ def do_macro(self, args: argparse.Namespace) -> None:
28932908
@as_subcommand_to('macro', 'create', macro_create_parser, help=macro_create_help)
28942909
def _macro_create(self, args: argparse.Namespace) -> None:
28952910
"""Create or overwrite a macro"""
2896-
28972911
# Validate the macro name
28982912
valid, errmsg = self.statement_parser.is_valid_command(args.name)
28992913
if not valid:
@@ -2966,17 +2980,19 @@ def _macro_create(self, args: argparse.Namespace) -> None:
29662980
break
29672981

29682982
# Set the macro
2969-
result = "overwritten" if args.name in self.macros else "created"
2983+
if not args.silent:
2984+
result = "overwritten" if args.name in self.macros else "created"
2985+
self.poutput("Macro '{}' {}".format(args.name, result))
2986+
29702987
self.macros[args.name] = Macro(name=args.name, value=value, minimum_arg_count=max_arg_num, arg_list=arg_list)
2971-
self.poutput("Macro '{}' {}".format(args.name, result))
29722988

29732989
# macro -> delete
29742990
macro_delete_help = "delete macros"
29752991
macro_delete_description = "Delete specified macros or all macros if --all is used"
29762992
macro_delete_parser = DEFAULT_ARGUMENT_PARSER(description=macro_delete_description)
2993+
macro_delete_parser.add_argument('-a', '--all', action='store_true', help="delete all macros")
29772994
macro_delete_parser.add_argument('names', nargs=argparse.ZERO_OR_MORE, help='macro(s) to delete',
29782995
choices_method=_get_macro_completion_items, descriptive_header='Value')
2979-
macro_delete_parser.add_argument('-a', '--all', action='store_true', help="delete all macros")
29802996

29812997
@as_subcommand_to('macro', 'delete', macro_delete_parser, help=macro_delete_help)
29822998
def _macro_delete(self, args: argparse.Namespace) -> None:
@@ -3002,21 +3018,29 @@ def _macro_delete(self, args: argparse.Namespace) -> None:
30023018
"Without arguments, all macros will be listed.")
30033019

30043020
macro_list_parser = DEFAULT_ARGUMENT_PARSER(description=macro_list_description)
3021+
macro_list_parser.add_argument('-w', '--with_silent', action='store_true',
3022+
help="include --silent flag with listed macros\n"
3023+
"Use this option when saving to a startup script that\n"
3024+
"should silently create macros.")
30053025
macro_list_parser.add_argument('names', nargs=argparse.ZERO_OR_MORE, help='macro(s) to list',
30063026
choices_method=_get_macro_completion_items, descriptive_header='Value')
30073027

30083028
@as_subcommand_to('macro', 'list', macro_list_parser, help=macro_list_help)
30093029
def _macro_list(self, args: argparse.Namespace) -> None:
30103030
"""List some or all macros"""
3031+
create_cmd = "macro create"
3032+
if args.with_silent:
3033+
create_cmd += " --silent"
3034+
30113035
if args.names:
30123036
for cur_name in utils.remove_duplicates(args.names):
30133037
if cur_name in self.macros:
3014-
self.poutput("macro create {} {}".format(cur_name, self.macros[cur_name].value))
3038+
self.poutput("{} {} {}".format(create_cmd, cur_name, self.macros[cur_name].value))
30153039
else:
30163040
self.perror("Macro '{}' not found".format(cur_name))
30173041
else:
30183042
for cur_macro in sorted(self.macros, key=self.default_sort_key):
3019-
self.poutput("macro create {} {}".format(cur_macro, self.macros[cur_macro].value))
3043+
self.poutput("{} {} {}".format(create_cmd, cur_macro, self.macros[cur_macro].value))
30203044

30213045
def complete_help_command(self, text: str, line: str, begidx: int, endidx: int) -> List[str]:
30223046
"""Completes the command argument of help"""
@@ -3051,12 +3075,12 @@ def complete_help_subcommands(self, text: str, line: str, begidx: int, endidx: i
30513075

30523076
help_parser = DEFAULT_ARGUMENT_PARSER(description="List available commands or provide "
30533077
"detailed help for a specific command")
3078+
help_parser.add_argument('-v', '--verbose', action='store_true',
3079+
help="print a list of all commands with descriptions of each")
30543080
help_parser.add_argument('command', nargs=argparse.OPTIONAL, help="command to retrieve help for",
30553081
completer_method=complete_help_command)
30563082
help_parser.add_argument('subcommands', nargs=argparse.REMAINDER, help="subcommand(s) to retrieve help for",
30573083
completer_method=complete_help_subcommands)
3058-
help_parser.add_argument('-v', '--verbose', action='store_true',
3059-
help="print a list of all commands with descriptions of each")
30603084

30613085
# Get rid of cmd's complete_help() functions so ArgparseCompleter will complete the help command
30623086
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)