Miscellaneous Features
Timer
Turn the timer setting on, and cmd2 will show the wall time it takes for each command to execute.
Exiting
Like many shell applications, cmd2 applications can be exited by pressing Ctrl-D on an empty
line, or by executing the quit command.
select
Presents numbered options to user, as bash select.
app.select is called from within a method (not by the user directly; it is app.select, not
app.do_select).
cmd2.Cmd.select
Present a menu to the user.
Modeled after the bash shell's SELECT. Returns the item chosen.
Argument opts can be:
| a single string -> will be split into one-word options | a list of strings -> will be offered as options | a list of tuples -> interpreted as (value, text), so that the return value can differ from the text advertised to the user
Source code in cmd2/cmd2.py
4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 | |
def do_eat(self, arg):
sauce = self.select("sweet salty", "Sauce? ")
result = "{food} with {sauce} sauce, yum!"
result = result.format(food=arg, sauce=sauce)
self.stdout.write(result + "\n")
See the do_eat method in the
read_input.py file for a
example of how to use `select.
Disabling Commands
cmd2 supports disabling commands during runtime. This is useful if certain commands should only be
available when the application is in a specific state. When a command is disabled, it will not show
up in the help menu or tab complete. If a user tries to run the command, a command-specific message
supplied by the developer will be printed. The following functions support this feature.
- enable_command : Enable an individual command
- enable_category : Enable an entire category of commands
- disable_command : Disable an individual command and set the message that will print when this command is run or help is called on it while disabled
- disable_category : Disable an entire category of commands and set the message that will print when anything in this category is run or help is called on it while disabled
See the definitions of these functions for descriptions of their arguments.
See the do_enable_commands() and do_disable_commands() functions in the
help_categories.py
example for a demonstration.