Python cmd line module

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Cmdline module wrapped around cmd

jeromebelleman/python-cmdline

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

A wrapper to Python’s built-in cmd module, offering a number of shortcuts and features, such as automatic argument parsing with argparse , paging in Vim, command line editing from the previous page, command completion alerting, command timing.

python-cmdline – Cmdline module

Assuming you’re writing a tool called foo after which you’ll name the Cmdline class:

#! /usr/bin/env python import sys import cmdline class Foo(cmdline.Cmdline): def __init__(self): cmdline.Cmdline.__init__(self) def main(): Foo().loop() if __name__ == '__main__': sys.exit(main())

directory : Directory in which the program directory lives. Defaults to the home directory.

history : Possible values are true and false, depending on whether you want to keep a command history or not. Default is false.

bell : Possible values are true and false, depending on whether you want to ring a bell at the end of each command completion. Default is false.

time : Possible values are true and false, depending on whether you want to add the —time option to each command to measure the time it takes to run it. Default is false.

Unlike Cmd, Cmdline expects command definitions in run_ functions taking an args namespace parameter:

def run_dothings(self, args): self.dothings(args.stuff)

An argument parser fooparser will be created for each command foo that will be defined as such.

If you need to add an argument to the command, do so by calling the foo.add_argument() function with the appropriate parameters.

Likewise, if you need to add a description to the command, do so by setting the foo.description string. (Not sure to which point one is supposed to do so with argparse but, but I haven’t found any better suggestion in the doc.)

Читайте также:  Функция создания переменных php

Some commodity functions are already defined:

run_EOF() : Close temporary files and exit.

run_edit() : Edit a command line.

run_page() : Page output.

Any of these can be overridden the usual way, should you need to add features. Example:

def run_EOF(self, _): clean_everything_up() cmdline.Cmdline.run_EOF(self, None)

The page command is made available to you for free, but you still need to implement what has to be paged. This is done by writing data to a temporary file which will be loaded into Vim next time you run page.

As the temporary file is kept open at all times, you need to run tempreset() to rewind and truncate it before writing anything to it. After this, it’s only a matter of writing to self.temp as well as to sys.stdout. There is no need to flush() because Cmdline does that anyway after each command runs.

About

Cmdline module wrapped around cmd

Источник

How to create a command-line utility using Python

Today I’ll show you how to make a command-line utility using Python. So maybe you are wondering what is a command-line utility? Right? So, let’s get started!

What is a command-line utility?

A command-line utility is a app that runs on the command-line. It does not have a GUI. It uses the command line interface or CLI to run and execute itself. If you want to read more about this, go here on wikipedia.

Prerequisites

I am going to be using Windows but the code will remain same for macOS and Linux as well. But the process for making a command-line utility are different. So, I guess I’ll make another post soon, for macOS and Linux. I am going to be using Visual Studio Code. You can use any code editor or text editor or IDE of your choice. Like, Sublime Text, Atom, Vim, etc. I will be using Command Prompt(CMD) as my Terminal, you can use Windows Powershell as well.

Terms that will be used

  • CMD — Command Prompt
  • CLI — Command Line Interface
  • CLU — Command Line Utility
  • VSCode — Visual Studio Code
  • py — python3
  • conda — anaconda3
  • editor, code editor, text editor, ide — Your editor

Modules

All modules used in this project are from the stdlib. No modules need to be installed seperately. Modules used are:

Project 1

What are we creating?

We are going to be making two utilities rather than one. The first one is going to be a file search utility that will search for a given keyword in file names in a specific directory, say, D: Drive. Then the utility lists all the files with the given word in their filename. And finally print the total no. of files that matched say, 23. And the fun part is we are not going to be taking these keyword and directory as input. Instead they should be passed as arguments after the utility name. And we are also going to take a look at Error and Exception Handling. For e.g. — Input:

C:\Users\username> search.py .py D:\Programming 
 D:\Programming\CLI-Utilities\search.py D:\Programming\CLI-Utilities\calc.py D:\Programming\etc.py No. of files matched = 3 
C:\Users\username> search.py python D:\Programming 
 No matching files in the given directory! Try another directory! 

Did you see that even though our file is in D:\Programming , we were access it from C:\Users\username ?

  1. Create a new folder named cli-utilities
  2. cd into that directory.
  3. Open it in your code editor. Type code . to open that directory in VSCode(if you are using VSCode).
  4. Make a new file named search.py .
  5. Now open search.py .
Читайте также:  Файл css размер шрифта

Actual Code

Ok, so let’s get into the fun stuff now.

First we are going to be importing the required modules.

# search.py - CLI-Utilities - play4Tutorials.hashnode.dev, github.com/play4Tutorials import os # stdlib import sys # stdlib 

Next we are gonna check if the modules were imported successfully. If not we are gonna handle the error or exception and print out some useful details. We are going to be using try/except for that.

try: import os # stdlib import sys # stdlib # Error and Exception Handling except ImportError as e: print("Some modules could not be imported from stdlib('sys' and 'os')") 

Next, we are going to be getting the arguments passed along with the filename. We are going to be using sys.argv for that. We are going to be storing them in variables. sys.argv returns a list of all the arguments passed. Among them the first one is the file name itself. So we are going to be starting from sys.argv[1].

keyword = sys.argv[1] # Get the keyword that is to be searched for 

Now, we have the keyword stored in the variable keyword . So, let’s get the path as well. But some times paths contain spaces, right? So, how do we deal with them? Since sys.argv splits arguments on spaces. Let’s see.

path = " ".join(sys.argv[2:]) # Get the path where to search for the keyword # Alright, so what we just did here is we used py's in-built `join()` function and joined # all the strings from index 2 to n with a space. So that now if the path contained # spaces, it will be joined with them. fileMatched = 0 # Initialize filesMatched as 0. I'll explain this later. 

Finally, we will now start creating the main function named search_files(keyword, path)

def search_files(keyword, path): filesFound = filesMatched for root, dirs, files in os.walk(path): # Use os.walk(path) to loop through the given directory and get the root, dirs, and files for file in files: # Loop through all the files in the given directory. if keyword in file: # Check if keyword is in filename filesFound += 1 # Counter to see how many files matched print(" "+root+'\\'+str(file)+"\n") # Print out the full file path for every matching file if filesFound == 0: # Check if no files matched the given keyword and print the same print("No matching files in the given directory! Try another directory!") # If 1 or more files are matched then print the same print(f"No. of files matched: ") 

The above code kind of explains itself, however I’ll explain it. First, we define a function named search_files(keyword, path) . Next, we create a new variable named filesFound which is equal to filesMatched . we can’t use filesMatched directly because then py would give an that it is an Unbound Variable . Ok, next up we create a for loop using os.walk(path)

for root, dirs, files in os.walk(path): 

So, what is os.walk() in Python? os.walk() is an in-built python method from the os module we imported at the beginning, remember? And it gives you back a list with all the names of the files in the given directory. And root, dirs, files is passed because it will return the root or the directory passed, then all the folders in the directory and then all the files in the directory. So we pass it store the values in the specific variables.

Читайте также:  Openserver update php version

Then, we have another loop in the files list or array

for file in files: if keyword in file: filesFound += 1 print(" " + root + '\\' + str(file) + "\n") if filesFound == 0: print("No matching files in the given directory! Try another directory!") 

This simply loops through all the files. And checks if the given keyword is present in the filename. If it is present, then filesFound is incremented by 1 and the whole path to the file is printed. The » «, «\n» are just make it look abit better in the CLI. And the «\\» (double back-slash) is used because single back-slash is used to escape the quotes but double back-slash escapes the slash. Finally, another if condition checks whether the no. of matched files is 0 or not? If iit is zero, we print the same. Else, we go out of the if statement, then out the if statement, then out of the loop, and again out of the loop.

print(f"No. of files matched: ") 

Over here, I am using f string to print out how many files matched the search. Using the filesFound variable. You can also use .format()

After that, we have another block of code for error and exception handling, which doesn’t require any explanation.

try: search_files(keyword, path) # Calling the function # Error and Exception Handling except FileNotFoundError as e: print("Error: FileNotFoundError occured! Make sure you entered the correct path and entered it correctly.") except Exception as e: print(f"Error: Some Error occured! \nDetails: ") 

Again, I am using f strings . You can use .format() as well.

And finally, the finished code looks like this.

# search.py - CLI-Utilities - play4Tutorials.hashnode.dev, github.com/play4Tutorials try: import os # stdlib import sys # stdlib # Exception and Error Handling except ImportError as e: print("Error: Some modules could not be imported from stdlib('sys' and 'os')") keyword = sys.argv[1] # Get the keyword that is to be searched for path = " ".join(sys.argv[2:]) # Get the path where to search for the keyword filesMatched = 0 # Initialize filesMatched as 0 # Function to search the keyword in a given directory def search_files(keyword, path): filesFound = filesMatched for root, dirs, files in os.walk(path): # Use os.walk(path) to loop through the given directory and get the root, dirs, and files for file in files: # Loop through all the files in the given directory. if keyword in file: # Check if keyword is in filename filesFound += 1 # Counter to see how many files matched print(" "+root+'\\'+str(file)+"\n") # Print out the full file path for every matching file if filesFound == 0: # Check if no files matched the given keyword and print the same print("No matching files in the given directory! Try another directory!") # If 1 or more files are matched then print the same print(f"No. of files matched: ") try: search_files(keyword, path) # Call the function # Error and Exception Handling except FileNotFoundError as e: print("Error: FileNotFoundError occured! Make sure you entered the correct path and entered it correctly.") except Exception as e: print(f"Error: Some Error occured! \nDetails: ") 

Moving on to the 2nd Project, which is super simple and easy and not as fun as the previous one.

Project 2

What are we making?

We are going to be making a simple CLI calculator. It will print out the result of the expression passed to it. For e.g. — Input:

C:\Users\username> calc.py 2 +10 * 5 

Источник

Оцените статью