changeset 328:7b2bca3951a7

locals_in_py
author cat@eee
date Fri, 12 Feb 2010 15:35:48 -0500
parents e9eea93c777c
children c69ad8418d39
files cmd2.py docs/freefeatures.rst docs/settingchanges.rst
diffstat 3 files changed, 55 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Thu Feb 11 18:22:17 2010 -0500
+++ b/cmd2.py	Fri Feb 12 15:35:48 2010 -0500
@@ -402,6 +402,7 @@
     feedback_to_output = False          # Do include nonessentials in >, | output
     quiet = False                       # Do not suppress nonessential output
     debug = False
+    locals_in_py = False
     settable = stubbornDict('''
         prompt
         colors                Colorized output (*nix only)
@@ -1030,13 +1031,16 @@
             interp = InteractiveInterpreter(locals=self.pystate)
             interp.runcode(arg)
         else:
-            interp = MyInteractiveConsole(locals=self.pystate)
+            localvars = (self.locals_in_py and self.pystate) or {}
+            interp = MyInteractiveConsole(locals=localvars)
             def quit():
+                'blah'
                 raise EmbeddedConsoleExit
             def onecmd(arg):
                 return self.onecmd(arg + '\n')
             self.pystate['quit'] = quit
             self.pystate['exit'] = quit
+            self.pystate['cmd'] = onecmd
             try:
                 cprt = 'Type "help", "copyright", "credits" or "license" for more information.'        
                 keepstate = Statekeeper(sys, ('stdin','stdout'))
--- a/docs/freefeatures.rst	Thu Feb 11 18:22:17 2010 -0500
+++ b/docs/freefeatures.rst	Fri Feb 12 15:35:48 2010 -0500
@@ -34,11 +34,59 @@
 Commands at invocation
 ======================
 
-TODO: broken!?
+You can send commands to your app as you invoke it by
+including them as extra arguments to the program.
+``cmd2`` interprets each argument as a separate 
+command, so you should enclose each command in 
+quotation marks if it is more than a one-word command.
+
+::
+
+  cat@eee:~/proj/cmd2/example$ python example.py "say hello" "say Gracie" quit
+  hello
+  Gracie
+  cat@eee:~/proj/cmd2/example$ 
 
 Python
 ======
 
+::
+
+The ``py`` command will run its arguments as a Python
+command.  Entered without arguments, it enters an
+interactive Python session.  That session can call
+"back" to your application with ``cmd("")``.  Through
+``self``, it also has access to your application
+instance itself.  (If that thought terrifies you,
+you can set the ``locals_in_py`` parameter to ``False``.
+See see :ref:`parameters`)
+
+::
+
+	(Cmd) py print("-".join("spelling"))
+	s-p-e-l-l-i-n-g
+	(Cmd) py
+	Python 2.6.4 (r264:75706, Dec  7 2009, 18:45:15) 
+	[GCC 4.4.1] on linux2
+	Type "help", "copyright", "credits" or "license" for more information.
+	(CmdLineApp)
+
+		py <command>: Executes a Python command.
+		py: Enters interactive Python mode.
+		End with `Ctrl-D` (Unix) / `Ctrl-Z` (Windows), `quit()`, 'exit()`.
+		Non-python commands can be issued with `cmd("your command")`.
+		
+	>>> import os
+	>>> os.uname()
+	('Linux', 'eee', '2.6.31-19-generic', '#56-Ubuntu SMP Thu Jan 28 01:26:53 UTC 2010', 'i686')
+	>>> cmd("say --piglatin {os}".format(os=os.uname()[0]))
+	inuxLay
+	>>> self.prompt
+	'(Cmd) '
+	>>> self.prompt = 'Python was here > '
+	>>> quit()
+	Python was here > 
+
 Searchable command history
 ==========================
 
--- a/docs/settingchanges.rst	Thu Feb 11 18:22:17 2010 -0500
+++ b/docs/settingchanges.rst	Fri Feb 12 15:35:48 2010 -0500
@@ -5,7 +5,7 @@
 Several aspects of a ``cmd2`` application's behavior
 can be controlled simply by setting attributes of ``App``.
 
-(To define your own user-settable parameters, see :ref:`parameters`
+(To define your own user-settable parameters, see :ref:`parameters`)
 
 Case-insensitivity
 ==================