From 63f0aa3256ef4422c2b3eab3d9ea0d44a15cc93e Mon Sep 17 00:00:00 2001 From: kotfu Date: Sun, 9 Sep 2018 20:05:08 -0600 Subject: Added/updated documentation for `Statement` --- docs/argument_processing.rst | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'docs') 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 -- cgit v1.2.1