Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aliases for commands with Python cmd module

How can I create an alias for a command in a line-oriented command interpreter implemented using the cmd module?

To create a command, I must implement the do_cmd method. But I have commands with long names (like constraint) and I want to provide aliases (in fact, shortcuts) for these commands (like co). How can I do that?

One possibility that came to my mind is to implement the do_alias (like do_co) method and just calling do_cmd (do_constraint) in this method. But this brings me new commands in the help of the CLI.

Is there any other way to achieve this? Or may be is there a way to hide commands from the help output?

like image 872
Simon Busard Avatar asked Oct 16 '12 09:10

Simon Busard


People also ask

How do I make an alias in CMD?

The alias syntax You type the word "alias", followed by the name you want to give the alias, stick in an = sign and then add the command you want it to run – generally enclosed in single or double quotes. Single word commands like "alias c=clear" don't require quotes.

Does CMD have alias?

An alias lets you create a shortcut name for a command, file name, or any shell text. By using aliases, you save a lot of time when doing tasks you do frequently. You can create a command alias. Use the alias Korn shell built-in command to define a word as an alias for some command.

What is the command alias for line?

In computing, alias is a command in various command-line interpreters (shells), which enables a replacement of a word by another string. It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command.

What is an alias command explain with example?

In Linux, an alias is a shortcut that references a command. An alias replaces a string that invokes a command in the Linux shell with another user-defined string. Aliases are mostly used to replace long commands, improving efficiency and avoiding potential spelling errors.


3 Answers

You can overwrite the default method and search for a suitable command handler (as already suggested by Brian):

import cmd

class WithAliasCmd(cmd.Cmd):

    def default(self, line):
        cmd, arg, line = self.parseline(line)
        func = [getattr(self, n) for n in self.get_names() if n.startswith('do_' + cmd)]
        if func: # maybe check if exactly one or more elements, and tell the user
            func[0](arg)
like image 136
sloth Avatar answered Nov 15 '22 20:11

sloth


The following solution makes aliased commands share a single help message. It lets you keep all of your aliases in a single easy to edit place, while making documentation much easier. It checks user input against an alias dictionary with function values and overrides both the default() (See sloth & brian) and do_help() methods.

Here I've made aliases 'c' and 'con' execute do_constraint(), 'q' invoke do_quit(), and 'h' invoke do_help(). The bonus of this solution is that 'h q' and 'help quit' print the same message. Documentation for several aliased commands can maintained in a single docstring.

import cmd

class prompt(cmd.Cmd):

    def __init__(self):
        cmd.Cmd.__init__(self)
        self.aliases = { 'c'   : self.do_constraint ,
                         'con' : self.do_constraint ,
                         'q'   : self.do_quit       ,
                         'h'   : self.do_help       }

    def do_constraint(self, arg):
        '''Constrain things.'''
        print('Constraint complete.')

    def do_quit(self, arg):
        '''Exit the program.'''
        return True

    def do_help(self, arg):
        '''List available commands.'''
        if arg in self.aliases:
            arg = self.aliases[arg].__name__[3:]
        cmd.Cmd.do_help(self, arg)

    def default(self, line):
        cmd, arg, line = self.parseline(line)
        if cmd in self.aliases:
            self.aliases[cmd](arg)
        else:
            print("*** Unknown syntax: %s" % line)
like image 38
hunkeyd Avatar answered Nov 15 '22 18:11

hunkeyd


The docs mention a default method, which you can override to handle any unknown command. Code it to prefix scan a list of commands and invoke them as you suggest for do_alias.

like image 23
Brian Avatar answered Nov 15 '22 18:11

Brian