How to create an interactive command line application? 
Author Message
 How to create an interactive command line application?

L.S.

How do I create an interactive command line application that I can
distribute as a single executable file?

Please note that I do not want to tell my customers:
"add an entry to your tclsh.rc script, which in turn calls script X",
because I expect them to be non-programmers. The application should run
out of the box, either as a single file executable or (next best), as a
single file executable Tcl script, as the example below)

I'm a bit surprised that this appears more difficult than expected,
because what I want is "just" a tclsh that has been adapted/extended
with a script.

The only thing I could come up with is writing my own loop to catch user
input from stdin (I appended a sample script below).

Am I right that this is really the way to go, or did I overlook the obvious?

Thanks in advance for any pointers,

Erik Leunissen
=============

#! /usr/bin/tclsh
set exposedCommands [list exit cmd1 cmd2 cmd3]

# Sample loop, retrieving user input:
while {1} {
     puts -nonewline stdout "MyApp > "
     flush stdout
     gets stdin line
     regexp {^([^ \t]+).*} $line match cmd
     if {[lsearch $exposedCommands $cmd] < 0} {
         puts stderr "Unknown command $cmd"
         continue
     }
     catch {
         eval $line
     } result
     puts $result

Quote:
}

--

e.          hccnet.   | respecting a character's position in a line.


Fri, 11 Aug 2006 03:15:08 GMT  
 How to create an interactive command line application?
You might want to check for [info complete] too... but otherwise, I know of no
other solution than you propsed, especially when filtering for exposed
commands.

Best regards, Richard Suchenwirth



Fri, 11 Aug 2006 05:56:36 GMT  
 How to create an interactive command line application?

Quote:

> L.S.

> How do I create an interactive command line application that I can
> distribute as a single executable file?

check out freewrap
http://freewrap.sourceforge.net/

-K_Lee
Tcl 8.4.4. Source Code RoadMap at http://www.slink-software.com/RM_tcl8.4.4.html



Fri, 11 Aug 2006 13:50:19 GMT  
 How to create an interactive command line application?

Quote:

> How do I create an interactive command line application that I can
> distribute as a single executable file?
...
> The only thing I could come up with is writing my own loop to catch
user
> input from stdin (I appended a sample script below).

> Am I right that this is really the way to go, or did I overlook the

obvious?

I don't know of a way to trick tclsh into first reading your script,
then dropping back into command line mode.  I don't think its set up
to do that.  But you could write your own custom tclsh, initialize it
with your startup code, then call Tcl_Main().

Or in pure Tcl, you can write a loop, as you propose, to handle
command line input.

Or if you can consider a Tk application, you can use the TkCon
console, which you _can_ fire up after running your script.  That
would give the users a command line (and lots of other features, like
history, etc.).  If you can't use TkCon directly, you might look at
its sources for command interpretation.

You might also consider using a child interpreter with only the
allowed commands.  Safe interpreters are well documented and quite
cool.  You can also attach a TkCon console to a child interpreter.

Bob
--

Mayo Foundation                                 (507) 538-5495
200 First St. SW                            FAX (507) 284-9171
Rochester MN, 55901  USA            http://www.mayo.edu/sppdg/



Sat, 12 Aug 2006 03:28:39 GMT  
 How to create an interactive command line application?

Quote:

> I don't know of a way to trick tclsh into first reading your script,
> then dropping back into command line mode.  I don't think its set up
> to do that.  But you could write your own custom tclsh, initialize it
> with your startup code, then call Tcl_Main().

Note that Tcl_Main() creates its own interp.  To influence how
that interp gets initialized, pass in an appropriate routine
as the Tcl_AppInit argument.

--
| Don Porter          Mathematical and Computational Sciences Division |

| http://math.nist.gov/~DPorter/                                  NIST |
|______________________________________________________________________|



Sat, 12 Aug 2006 03:40:27 GMT  
 How to create an interactive command line application?
This will most likely be the way for you to go. Starkits and starpacks are
way cool.

http://www.equi4.com/starkit.html

Quote:

> L.S.

> How do I create an interactive command line application that I can
> distribute as a single executable file?

> Please note that I do not want to tell my customers:
> "add an entry to your tclsh.rc script, which in turn calls script X",
> because I expect them to be non-programmers. The application should run
> out of the box, either as a single file executable or (next best), as a
> single file executable Tcl script, as the example below)

> I'm a bit surprised that this appears more difficult than expected,
> because what I want is "just" a tclsh that has been adapted/extended
> with a script.

> The only thing I could come up with is writing my own loop to catch user
> input from stdin (I appended a sample script below).

> Am I right that this is really the way to go, or did I overlook the obvious?

> Thanks in advance for any pointers,

> Erik Leunissen
> =============

> #! /usr/bin/tclsh
> set exposedCommands [list exit cmd1 cmd2 cmd3]

> # Sample loop, retrieving user input:
> while {1} {
>      puts -nonewline stdout "MyApp > "
>      flush stdout
>      gets stdin line
>      regexp {^([^ \t]+).*} $line match cmd
>      if {[lsearch $exposedCommands $cmd] < 0} {
>          puts stderr "Unknown command $cmd"
>          continue
>      }
>      catch {
>          eval $line
>      } result
>      puts $result
> }



Sat, 12 Aug 2006 08:47:25 GMT  
 How to create an interactive command line application?


:How do I create an interactive command line application that I can
:distribute as a single executable file?

What do you mean, when you refer to an 'interactive command line application'?

Do you mean an application which the user invokes from the command line?

I'm uncertain what it is you hope to get.

However, note that the base level tcl from the source distribution
is not designed to create a single executable file.  Instead, it is
designed to provide an interpreter which expects dlls or shared libraries,
various script and encoding files, etc.

Tools such as freewrap, starpacks, etc. provide the capability of taking
various sets of files and producing a single executable file as a result.

:Please note that I do not want to tell my customers:
:"add an entry to your tclsh.rc script, which in turn calls script X",

Hmm - I'm not certain what you mean by this.  The only reason to modify
tclsh.rc is if your intention is to provide a user with some code they
would use during interactions with a tclsh interpreter internal
command loop.  That's the only time that file is used.

From the trailing example, it almost looks like your customers are going
to be sitting at a tcl prompt.  Sounds to me as if the simplest thing
for you would be to take the main .c file from tclsh (or the tk main
.c file if you expect your customers to use Tk), and modify it to
invoke the code you want executed.
--
<URL: http://wiki.tcl.tk/ > In God we trust.
Even if explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.



Sun, 13 Aug 2006 03:33:04 GMT  
 How to create an interactive command line application?

Quote:

> How do I create an interactive command line application that I can distribute
> as a single executable file?

See the freewraptclsh.  New version of freewrap just announced.




Sun, 13 Aug 2006 04:22:24 GMT  
 How to create an interactive command line application?

Quote:


> :How do I create an interactive command line application that I can
> :distribute as a single executable file?

> What do you mean, when you refer to an 'interactive command line application'?

> Do you mean an application which the user invokes from the command line?

> I'm uncertain what it is you hope to get.

[snipped some]
I was inaccurate indeed. I meant what you already deduced correctly from
the sample code:

  an interactive tclsh application, modified to execute custom commands.

Quote:

> Hmm - I'm not certain what you mean by this.  The only reason to modify
> tclsh.rc is if your intention is to provide a user with some code they
> would use during interactions with a tclsh interpreter internal
> command loop.  That's the only time that file is used.

That's exactly what I intended to provide.

Quote:

> From the trailing example, it almost looks like your customers are going
> to be sitting at a tcl prompt.

Correct.

   Sounds to me as if the simplest thing

Quote:
> for you would be to take the main .c file from tclsh (or the tk main
> .c file if you expect your customers to use Tk), and modify it to
> invoke the code you want executed.

Thanks,

Erik.

--

e.          hccnet.   | respecting a character's position in a line.



Sun, 13 Aug 2006 05:00:15 GMT  
 How to create an interactive command line application?

Quote:

> Instead, it is
> designed to provide an interpreter which expects dlls or shared libraries,
> various script and encoding files, etc.

Well, that about summarizes why I was a bit surprised:

A Tk script (invoking wish) ends up entering the event loop, which
allows users to interact with the interpreter (in most cases through a
gui), executing custom commands. No additional tricks are needed.

However, a tcl script (invoking tclsh), can provide this functionality
only through:
- access of a user's preferences file
- writing your own loop to fetch commands at the prompt, or,
- providing custom C-code for tclsh.

I somehow was hoping to find a way analoguous to the Tk case, where
tclsh executes a Tcl script ending up in the command loop.

Erik.

--

e.          hccnet.   | respecting a character's position in a line.



Sun, 13 Aug 2006 05:42:58 GMT  
 How to create an interactive command line application?
Quote:

> L.S.

> How do I create an interactive command line application that I can
> distribute as a single executable file?

> Please note that I do not want to tell my customers:
> "add an entry to your tclsh.rc script, which in turn calls script X",
> because I expect them to be non-programmers. The application should run
> out of the box, either as a single file executable or (next best), as a
> single file executable Tcl script, as the example below)

> I'm a bit surprised that this appears more difficult than expected,
> because what I want is "just" a tclsh that has been adapted/extended
> with a script.

> The only thing I could come up with is writing my own loop to catch user
> input from stdin (I appended a sample script below).

> Am I right that this is really the way to go, or did I overlook the
> obvious?

> Thanks in advance for any pointers,

> Erik Leunissen
> =============

> #! /usr/bin/tclsh
> set exposedCommands [list exit cmd1 cmd2 cmd3]

> # Sample loop, retrieving user input:
> while {1} {
>     puts -nonewline stdout "MyApp > "
>     flush stdout
>     gets stdin line
>     regexp {^([^ \t]+).*} $line match cmd
>     if {[lsearch $exposedCommands $cmd] < 0} {
>         puts stderr "Unknown command $cmd"
>         continue
>     }
>     catch {
>         eval $line
>     } result
>     puts $result
> }

You could use the stdin package and rename (hide) commands you don't
wish your users to access.

I use the following at the end of tclsh scripts for debugging and
testing interactively (taken from tclhhttpd).

# Enter Event loop
if {$Config(debug) } {
     package require stdin
     Stdin_Start "Triggr % "

Quote:
} else {

     # Trap SIGTERM.
     # socket -server {} 0 ; # Trick to vwait until we setup real event
     vwait appconf(wait)

- Show quoted text -

Quote:
}



Sun, 13 Aug 2006 16:21:15 GMT  
 How to create an interactive command line application?


:I was inaccurate indeed. I meant what you already deduced correctly from
:the sample code:
:
:  an interactive tclsh application, modified to execute custom commands.
:
:>
:> From the trailing example, it almost looks like your customers are going
:> to be sitting at a tcl prompt.
:
:Correct.

Does the user have the ability to display a graphical interface?  If so,
then it might be useful to take a look at Jeff Hobbes' tkcon program,
and consider using it as the base of your distribution.  It provides
a nice interface to tclsh and includes a variety of useful additional
functions.
--
<URL: http://wiki.tcl.tk/ > In God we trust.
Even if explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.



Sun, 13 Aug 2006 22:14:05 GMT  
 How to create an interactive command line application?

Quote:


>> Instead, it is
>> designed to provide an interpreter which expects dlls or shared
>> libraries, various script and encoding files, etc.

> Well, that about summarizes why I was a bit surprised:

> A Tk script (invoking wish) ends up entering the event loop, which
> allows users to interact with the interpreter (in most cases through a
> gui), executing custom commands. No additional tricks are needed.

> However, a tcl script (invoking tclsh), can provide this functionality
> only through:
> - access of a user's preferences file
> - writing your own loop to fetch commands at the prompt, or,
> - providing custom C-code for tclsh.

> I somehow was hoping to find a way analoguous to the Tk case, where
> tclsh executes a Tcl script ending up in the command loop.

I think your best bet is to go with tkcon, but you can do what you
want with a more elaborate event loop script in tcl.  tclsh is
meant to be either interactive or not, and by giving it a script
you are telling it to not be interactive.  Simulating that doesn't
take all that many lines of Tcl, and I have the code around
somewhere (I'm certain I've posted it before).  I still favor the
tkcon approach because of all the extra advantages it gives,
especially for new users (colorizing is very helpful that way).

--
     Jeff Hobbs, The Tcl Guy
     http://www.ActiveState.com/, a division of Sophos



Mon, 14 Aug 2006 00:42:28 GMT  
 How to create an interactive command line application?

Quote:

> I think your best bet is to go with tkcon, but you can do what you
> want with a more elaborate event loop script in tcl.  tclsh is
> meant to be either interactive or not, and by giving it a script
> you are telling it to not be interactive.  Simulating that doesn't
> take all that many lines of Tcl, and I have the code around
> somewhere (I'm certain I've posted it before).

I'm not sure whether you refer to code like what's in tkcon (using a
while loop) or the stdin package inside tlhttpd (using the event loop to
process file-events on stdin) or that you refer something entirely
different.

I can proceed nicely with any of these as a basis (while still
considering to build upon tkcon).

However, if you're talking about a mechanism different from these, then
I am very much interested in having a look at it.

Erik.

   I still favor the

Quote:
> tkcon approach because of all the extra advantages it gives,
> especially for new users (colorizing is very helpful that way).

--

e.          hccnet.   | respecting a character's position in a line.


Mon, 14 Aug 2006 04:56:34 GMT  
 How to create an interactive command line application?

Quote:


>> I think your best bet is to go with tkcon, but you can do what you
>> want with a more elaborate event loop script in tcl.  tclsh is
>> meant to be either interactive or not, and by giving it a script
>> you are telling it to not be interactive.  Simulating that doesn't
>> take all that many lines of Tcl, and I have the code around
>> somewhere (I'm certain I've posted it before).

> I'm not sure whether you refer to code like what's in tkcon (using a
> while loop) or the stdin package inside tlhttpd (using the event loop to
> process file-events on stdin) or that you refer something entirely
> different.

No, I'm refering to a fileevent based solution.

--
     Jeff Hobbs, The Tcl Guy
     http://www.ActiveState.com/, a division of Sophos



Mon, 14 Aug 2006 13:19:35 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. Edit command line in interactive tclsh

2. passing arguments to an application from the command-line

3. integrating a tcl command line interpretor in an MFC Application

4. win32: I try to script a command-line application on Windows2000, no luck so far

5. how to create a new application with a dictionnary already created

6. Clarion Application Profile Exchange - Advertise your Clarion Created Application

7. Creating stand applications applications in MICRO FOCUS?

8. reading a line and executing a command on that line

9. Win2000 Gawk Renaming a file with system() command, command line to long

10. command environments and command line arguments

11. Command line variables to executables,command programs

12. command line options for old viscob and vclink commands

 

 
Powered by phpBB® Forum Software