Popular recipes tagged "cli"http://code.activestate.com/recipes/tags/cli/2017-03-31T14:30:30-07:00ActiveState Code RecipesUnix tee-like functionality via a Python class (Python)
2017-03-31T14:30:30-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580767-unix-tee-like-functionality-via-a-python-class/
<p style="color: grey">
Python
recipe 580767
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tee/">tee</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>The Unix tee commmand, when used in a command pipeline, allows you to capture the output of the preceding command to a file or files, while still sending it on to standard output (stdout) for further processing via other commands in a pipeline, or to print it, etc.</p>
<p>This recipe shows how to implement simple tee-like functionality via a Python class. I do not aim to exactly replicate the functionality of the Unix tee, only something similar.</p>
<p>More details and sample output here:</p>
<p><a href="https://jugad2.blogspot.in/2017/03/a-python-class-like-unix-tee-command.html" rel="nofollow">https://jugad2.blogspot.in/2017/03/a-python-class-like-unix-tee-command.html</a></p>
Give Python code a web plus command-line interface with hug (Python)
2017-01-05T16:57:15-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580742-give-python-code-a-web-plus-command-line-interface/
<p style="color: grey">
Python
recipe 580742
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/hug/">hug</a>, <a href="/recipes/tags/library/">library</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/user_interface/">user_interface</a>, <a href="/recipes/tags/web/">web</a>, <a href="/recipes/tags/web_server/">web_server</a>).
</p>
<p>This recipe shows how to take a Python function and wrap it with both a web and a command-line interface, somewhat easily, using the hug Python library. The example used shows how to wrap a function that uses the psutil library to get information on disk partitions. So you can see the disk partition info either via the web browser or the command line. The code for the recipe is shown below. It is also possible to wrap multiple functions in the same Python file, and expose all of them via both the web and the command-line.</p>
<p>More information and multiple sample outputs are available here:</p>
<p><a href="https://jugad2.blogspot.in/2017/01/give-your-python-function-webcli-hug.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/give-your-python-function-webcli-hug.html</a></p>
Progress bar class (Python)
2012-08-09T17:39:10-07:00Xavier L.http://code.activestate.com/recipes/users/4171602/http://code.activestate.com/recipes/578228-progress-bar-class/
<p style="color: grey">
Python
recipe 578228
by <a href="/recipes/users/4171602/">Xavier L.</a>
(<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/curses/">curses</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/module/">module</a>, <a href="/recipes/tags/progress/">progress</a>).
Revision 5.
</p>
<p>See <a href="https://gist.github.com/3306295">gist:3306295</a> for future developments.</p>
<p>Here is a little class that lets you present percent complete information in the form of a progress bar using the '=' character to represent completed portions, spaces to represent incomplete portions, '>' to represent the current portion and the actual percent done (rounded to integer) displayed at the end:</p>
<p>[===========> ] 60%</p>
<p>When you initialize the class, you specify the minimum number (defaults to 0), the maximum number (defaults to 100), and the desired width of the progress bar. The brackets <code>[]</code> are included in the size of the progress bar, but you must allow for up to 4 characters extra to display the percentage.</p>
<p>You'd probably want to use this in conjuction with the curses module, or something like that so you can over-write the same portion of the screen to make your updates 'animated'.</p>
Generate automatically list of actions with argparse (Python)
2011-12-02T10:25:13-08:00Andrea Crottihttp://code.activestate.com/recipes/users/4179946/http://code.activestate.com/recipes/577947-generate-automatically-list-of-actions-with-argpar/
<p style="color: grey">
Python
recipe 577947
by <a href="/recipes/users/4179946/">Andrea Crotti</a>
(<a href="/recipes/tags/argparse/">argparse</a>, <a href="/recipes/tags/cli/">cli</a>).
Revision 2.
</p>
<p>This little sample script looks up in locals() to determine all the callables in <em>main</em>.</p>
<p>Then it passes them to parse_arguments, which show an help message as following
usage:</p>
<pre class="prettyprint"><code>prova.py [-h] {func2,func1}
positional arguments:
{func2,func1}
optional arguments:
-h, --help show this help message and exit
</code></pre>
<p>Useful to avoid duplication and make it easy to extend script functions.</p>
query yes/no (Python)
2010-03-09T17:57:28-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577058-query-yesno/
<p style="color: grey">
Python
recipe 577058
by <a href="/recipes/users/4173505/">Trent Mick</a>
(<a href="/recipes/tags/ask/">ask</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/no/">no</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/raw_input/">raw_input</a>, <a href="/recipes/tags/yes/">yes</a>).
Revision 2.
</p>
<p>Ask the user a question using raw_input() and looking something
like this:</p>
<pre class="prettyprint"><code>QUESTION [Y/n]
...validate...
</code></pre>
<p>See also: <a href="http://code.activestate.com/recipes/577096/">Recipe 577096</a> (query custom answers), <a href="http://code.activestate.com/recipes/577097/">Recipe 577097</a> (query yes/no/quit), <a href="http://code.activestate.com/recipes/577098/">Recipe 577098</a> (query long), <a href="http://code.activestate.com/recipes/577099/">Recipe 577099</a> (query)</p>
transform command line arguments to args and kwargs for a method call (Python)
2010-03-21T22:41:42-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577122-transform-command-line-arguments-to-args-and-kwarg/
<p style="color: grey">
Python
recipe 577122
by <a href="/recipes/users/4173505/">Trent Mick</a>
(<a href="/recipes/tags/argv/">argv</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/json/">json</a>).
Revision 2.
</p>
<p>For many of my Python modules I find it convenient to provide a small <code>if __name__ == "__main__": ...</code> block to be able to call individual methods from the command line. This requires some kind of translation of command-line string arguments to <code>args</code> and <code>kwargs</code> for the method call. This recipe uses a few conventions to do that:</p>
<ul>
<li>the first argument is the method name</li>
<li>positional args are positional (duh)</li>
<li>"key=value" is a keyword argument</li>
<li>an attempt is made to interpret arguments as JSON to allow specifying types other than string</li>
</ul>
file path generator from path patterns (Python)
2010-07-09T19:10:59-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577230-file-path-generator-from-path-patterns/
<p style="color: grey">
Python
recipe 577230
by <a href="/recipes/users/4173505/">Trent Mick</a>
(<a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/paths/">paths</a>, <a href="/recipes/tags/walk/">walk</a>).
Revision 4.
</p>
<p>Provides a <code>_paths_from_path_patterns</code> that will generate a list of paths from a list of path patterns. A "path pattern" can include glob chars. By default it generates a recursive listing of file paths, but: recursion can be turned off, file and/or dir paths can be listed. It supports a list of glob exclusions or inclusions.</p>
<p>This function makes it easy to implement typical "-r|--recursive" and "-x|--exclude" options for command-line scripts that work on given file paths. See <a href="#block-1">example usages below</a>.</p>
<p>Note: I use a leading <code>_</code> on function names because my typical usage of my recipes is as re-usable <em>internal</em> functions in Python modules.</p>
query yes/no/quit (Python)
2010-03-09T17:57:18-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577097-query-yesnoquit/
<p style="color: grey">
Python
recipe 577097
by <a href="/recipes/users/4173505/">Trent Mick</a>
(<a href="/recipes/tags/ask/">ask</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/no/">no</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/quit/">quit</a>, <a href="/recipes/tags/raw_input/">raw_input</a>, <a href="/recipes/tags/yes/">yes</a>).
</p>
<p>Ask the user a question using raw_input() and looking something
like this:</p>
<pre class="prettyprint"><code>QUESTION [Y/n/q]
...validate...
</code></pre>
<p>See also: <a href="http://code.activestate.com/recipes/577058/">Recipe 577058</a> (query yes/no), <a href="http://code.activestate.com/recipes/577096/">Recipe 577096</a> (query custom answers), <a href="http://code.activestate.com/recipes/577098/">Recipe 577098</a> (query long), <a href="http://code.activestate.com/recipes/577099/">Recipe 577099</a> (query)</p>
command line query (Python)
2010-03-09T18:37:52-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577098-command-line-query/
<p style="color: grey">
Python
recipe 577098
by <a href="/recipes/users/4173505/">Trent Mick</a>
(<a href="/recipes/tags/ask/">ask</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/raw_input/">raw_input</a>).
Revision 3.
</p>
<p>Ask the user a question using raw_input() and looking something
like this (<code>style=="compact"</code>):</p>
<pre class="prettyprint"><code>QUESTION [DEFAULT]: _
...validation...
</code></pre>
<p>or this (<code>style=="verbose"</code>):</p>
<pre class="prettyprint"><code>QUESTION
Hit <Enter> to use the default, DEFAULT.
> _
...validate...
</code></pre>
<p>It supports some basic validation/normalization of the given answer.</p>
<p>See also: <a href="http://code.activestate.com/recipes/577058/">Recipe 577058</a> (query yes/no), <a href="http://code.activestate.com/recipes/577097/">Recipe 577097</a> (query yes/no/quit), <a href="http://code.activestate.com/recipes/577096/">Recipe 577096</a> (query custom answers)</p>
query custom answers (Python)
2010-03-09T17:57:22-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577096-query-custom-answers/
<p style="color: grey">
Python
recipe 577096
by <a href="/recipes/users/4173505/">Trent Mick</a>
(<a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/query/">query</a>).
</p>
<p>Ask the user a question using raw_input() and looking something
like this:</p>
<pre class="prettyprint"><code>Frob nots the zids? [Yes (default), No, quit] _
...validate...
</code></pre>
<p>See also: <a href="http://code.activestate.com/recipes/577058/">Recipe 577058</a> (query yes/no), <a href="http://code.activestate.com/recipes/577097/">Recipe 577097</a> (query yes/no/quit), <a href="http://code.activestate.com/recipes/577098/">Recipe 577098</a> (query long), <a href="http://code.activestate.com/recipes/577099/">Recipe 577099</a> (query)</p>
query (Python)
2010-03-09T18:34:05-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577099-query/
<p style="color: grey">
Python
recipe 577099
by <a href="/recipes/users/4173505/">Trent Mick</a>
(<a href="/recipes/tags/ask/">ask</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/raw_input/">raw_input</a>).
</p>
<p>Ask/prompt the user for a short piece of data. <a href="http://code.activestate.com/recipes/577098/">Recipe 577098</a> is a much more generic/functional (but longer) take on this.</p>
Send Email (Python)
2012-08-26T11:23:54-07:00sfwgeekhttp://code.activestate.com/recipes/users/4170734/http://code.activestate.com/recipes/576807-send-email/
<p style="color: grey">
Python
recipe 576807
by <a href="/recipes/users/4170734/">sfwgeek</a>
(<a href="/recipes/tags/argparse/">argparse</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/email/">email</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/line/">line</a>, <a href="/recipes/tags/mail/">mail</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/send/">send</a>, <a href="/recipes/tags/smtplib/">smtplib</a>).
Revision 5.
</p>
<p>A Command Line Interface (CLI) program to send email.</p>
Automated management of CLI devices (Python)
2009-05-27T01:24:12-07:00Alan Holthttp://code.activestate.com/recipes/users/4170480/http://code.activestate.com/recipes/576778-automated-management-of-cli-devices/
<p style="color: grey">
Python
recipe 576778
by <a href="/recipes/users/4170480/">Alan Holt</a>
(<a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/management/">management</a>, <a href="/recipes/tags/ssh/">ssh</a>, <a href="/recipes/tags/telnet/">telnet</a>, <a href="/recipes/tags/tunnel/">tunnel</a>).
</p>
<p>This recipe provides a mechanism for remote, automated control of a network device via its command-line interface (CLI). It assumes that the CLI is accessed using Telnet. Furthermore, the device cannot be accessed directly, instead the user has to SSH to an intermediate jump host before Telneting to the device. </p>