diff options
| author | kotfu <kotfu@kotfu.net> | 2018-09-09 20:05:08 -0600 |
|---|---|---|
| committer | kotfu <kotfu@kotfu.net> | 2018-09-09 20:05:08 -0600 |
| commit | 63f0aa3256ef4422c2b3eab3d9ea0d44a15cc93e (patch) | |
| tree | 8c3f93f3deb4a02ac990c23632d1e4ed6069c841 /docs/argument_processing.rst | |
| parent | e2adc8fe379aa40142d8fee1414a5b563084b704 (diff) | |
| download | cmd2-git-63f0aa3256ef4422c2b3eab3d9ea0d44a15cc93e.tar.gz | |
Added/updated documentation for `Statement`
Diffstat (limited to 'docs/argument_processing.rst')
| -rw-r--r-- | docs/argument_processing.rst | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/docs/argument_processing.rst b/docs/argument_processing.rst index 5aef3720..022ea4d6 100644 --- a/docs/argument_processing.rst +++ b/docs/argument_processing.rst @@ -296,7 +296,36 @@ Receiving an argument list ========================== The default behavior of ``cmd2`` is to pass the user input directly to your -``do_*`` methods as a string. If you don't want to use the full argument parser support outlined above, you can still have ``cmd2`` apply shell parsing rules to the user input and pass you a list of arguments instead of a string. Apply the ``@with_argument_list`` decorator to those methods that should receive an argument list instead of a string:: +``do_*`` methods as a string. The object passed to your method is actually a +``Statement`` object, which has additional attributes that may be helpful, +including ``arg_list`` and ``argv``:: + + class CmdLineApp(cmd2.Cmd): + """ Example cmd2 application. """ + + def do_say(self, statement): + # statement contains a string + self.poutput(statement) + + def do_speak(self, statement): + # statement also has a list of arguments + # quoted arguments remain quoted + for arg in statement.arg_list: + self.poutput(arg) + + def do_articulate(self, statement): + # statement.argv contains the command + # and the arguments, which have had quotes + # stripped + for arg in statement.argv: + self.poutput(arg) + + +If you don't want to access the additional attributes on the string passed to +you``do_*`` method you can still have ``cmd2`` apply shell parsing rules to the +user input and pass you a list of arguments instead of a string. Apply the +``@with_argument_list`` decorator to those methods that should receive an +argument list instead of a string:: from cmd2 import with_argument_list |
