Module 3: Command line

The command line used to the very traditional way of interacting with computers. Now, it is an odd little thing that many haven't heard about and not commonly used. Programmers have more opportunity to use command line sessions, but even then it may be for just a few random tasks.

However, when the command line comes up as needed it generally is the only way to accomplish a task.

What goes into a command line tool?

Just from the perspective of the user, there's usually the following elements. Note that the command line space is a highly flexible/powerful/varied sort of thing. There are exceptions, conventions, gray areas, etc. everywhere. So the below is generally true and meant to provide a framework of understanding, not a hard set of rules. In particular, order/number/options across all elements can be wild.

  • the name of the program or tool at the start

  • the name of a file or something else telling the program what to act on

  • the name of a specific action the tool should take

  • flags or other modifiers that set parameters, modifications, etc of how the operations should run

    • Usually these have - in front of them, but others are used.

  • whitespace between content and flag syntax can vary depending on tool, but most of the time it is very meaningful and tools can be very sensitive to white space

    • Some will be very strict about spaces (exactly one, none allowed, etc.)

    • Some can be more flexible

Generally, remember that you are working with a system that is being parsed by the command line system plus the program you are calling to run.

As dictated by the tool

Many tools are specifically created to be used on the command line and may not give you a bunch of choice.

I'm honestly not sure why, but many of the image and document format conversion tools do this. Normally this is fine because we commonly just need to convert something specific and move on.

Bringing these things into automation can be tricky, but usually accomplishable in pure python.

As dictated by the problem you made

Sometimes you just break your computer or just do something else stupid. Things may be running slowly etc., but when they are usually terminal or other command line tool is still available when others aren't. You can use it to delete or move files that may be causing the issue, etc.

Alternatively, there are command line versions of things to take over when the application version of the program is no longer working. For example, GitHub Desktop can start crashing with large number of files to commit and push, etc. There are also specific advanced techniques you may only be able to do on the command line with it. Luckily, each system is just an interface for the repositories so you can use the application and command line in tandem.

Command line and python

Python itself can be run as a command line application. https://docs.python.org/3/using/cmdline.html

Scripts can be written specifically to be run this way, with some very nice tools for parsing arguments etc. (https://docs.python.org/3/library/sys.html#sys.argv).

Python also supports many tools for interacting with the command line system of a computer in an automated way. (https://docs.python.org/3/library/subprocess.html) Notably, you can have it issue out shell-like commands. This allows you to take a tool meant to process a single thing somewhere on the command line and run multiple things through it. You may find it better to write a shell script for such things, but always worth remembering that this is an option.

Even more modules provide support for parsing text and commands in a shell-like way, treating that text input in a very object oriented way. (https://docs.python.org/3/library/shlex.html) This saves you the work and grief of regex.

Many times there are several module and tools that can accomplish the same sort of task. There will be differences in complexity and caveats to each, so don't discount things that may look like duplicates.

There's a very nice discussion about techniques here: https://janakiev.com/blog/python-shell-commands/

At the end of the day, why?

Well, it'll come up. Usually when it comes up it is something that will completely block you for proceeding unless you handle it. Some kinds of work will make the command line quite rare while others will regularly need it. Some people also just prefer the command line for certain tasks.

The good news is that you don't need a huge amount of knowledge and experience to be good enough to accomplish a lot of things.

Last updated