Console Apps
Podcast: Play in new window | Download (46.4MB) | Embed
Subscribe: Apple Podcasts | Spotify | Email | RSS | More
“If you started out in software development ‘back in the day’, you probably got some early exposure to console applications.”
Console applications are often the simplest place to start when learning to develop software for desktop and server environments, requiring far less ceremony to get going. Generally speaking, you learn just enough console stuff to get the basics of a language down and then you move on to graphical applications, as those are more often in demand. As we go along through our careers, we often forget about the things we can do on the console to make life easier for ourselves.
“I think a lot of developers are getting pushed toward web develoment.”
Having a better grasp of what is going on under the hood with console applications may help you significantly in your day job, even if you are primarily a web developer. If you are using tools like the angular cli, for instance, it can help you reason about what the tool is actually doing and make invoking it make a little more sense.
Episode Breakdown
08:25 What is a Console or Terminal Application?
“If you like to torture yourself, like Will, you will learn to use a graphical interface without the mouse.”
From Wikipedia: A console application is a computer program designed to be used via a text-only computer interface, such as a text terminal, the command line interface of some operating systems (Unix, DOS, etc.) or the text-based interface included with most Graphical User Interface (GUI) operating systems, such as the Win32 console in Microsoft Windows, the Terminal in Mac OS X, and xterm in Unix.
A user typically interacts with a console application using only a keyboard and display screen, as opposed to GUI applications, which normally require the use of a mouse or other pointing device.
Terminology
09:50 Application Name
“You type magic words into the black box with white letters.”
The application itself in most terminals and consoles is the first thing specified on the command line. What can be executed is specified in different ways on different systems. Windows tends to do this by file extension. Extensions like .exe, .bat, .cmd, and .ps1. Unix-based systems tend to do this by setting a flag on the file to indicate that it can be executed.
11:43 Arguments
“These are like parameters going into a function.”
After the application name come the arguments that will be passed into the console app. At the simplest case, these arguments can be simple switches to turn functionality on or off. They can also be several levels deep, as if there were a tree of options. The arguments to an application can include the output from other applications. You can also stream data from a file into an application.
“Is that get with an ‘e’ or an ‘i’?”
It’s unwise to make these arguments require characters that have to be escaped on the commandline. Linux apps tend to use a dash before arguments and windows apps tend to use a slash “/” before arguments.
15:48 Shell
The shell is a common execution environment for console applications. That’s the little black window in windows or DOS if you are old enough to remember it. The shell is essentially the top level process that is orchestrating the other things you are working with.
18:00 Pipes
A pipe is a method of interprocess communication. Essentially what it does is that it takes data in and data comes out of it in a first in first out manner (FIFO). An app can open it to read and another app can open it to write. What is written by the second app is read out by the first in the same order it was sent in. You can stream into and out of files.
“Asyncronous water park slides are not fun.”
In windows, users typically don’t see these, although they are available in the NPFS (named pipe file system). In unix, it’s a core feature that users are expected to interact with, frequently. It may be helpful to think of them the same way you think about streams in your favorite programming language.
22:30 Child Process
A process that is started by another process. Child processes are stopped when the parent process is stopped. This can be done either by the operating system’s API, via its GUI, or by a termination sequence (CTRL + C on windows terminal).
24:53 Standard Streams
Pre-connected input and output channels between a computer program and its execution environment. When using the shell, these are typically connected to the text terminal. Generally, child processes inherit the standard streams of the parent process.
“It’s like this except when it’s not like this.”
StdIn or the standard input is what goes into the program. This input may be sent from the keyboard or directed from a pipe. StdOut or the standard output is what comes out of a program. Typically this goes out to the terminal, but can also be piped to a file or another program. StdErr or standard error is a secondary output stream used to indicate errors. It may be redirected to the same destination as stdout. You’ll see different stream notation on other systems such as VMS.
27:10 Exit Codes
Indicate the result of a process. Typically zero means it was ok. Nonzero means something else. The exit is often an error number.
28:36 Environmental Variables
Dynamic variable that can effect the way a process runs. They tend to be key-value pair and can be system or user level. They might have multiple things in them like windows PATH variable.
30:45 POSIX
The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines the application programming interface (API), along with command line shells and utility interfaces, for software compatibility with variants of Unix and other operating systems.
“These were built when the trust between machines was much higher.”
There are varying levels of compliance in various unixes, MacOS, BSDs, etc. Windows isn’t compliant but it wouldn’t surprise me if they eventually got there.
Common Practices
33:55 Switches
Command line switches vary, but are organized around being capable of being parsed by text libraries. This typically means that switches are delimited in some fashion, usually by spaces.
“I think it was Hexo that I’ve used most recently that did that.”
You typically will have a switch that lets you find out what parameters are supported. It will often be -h(elp), /h(elp) or /?. Additionally, if someone enters incorrect parameters, it’s generally best practice to tell them what is supported. There may/should be both long and short forms of switches, like /h and /help.
35:50 Command Tree
If the app does a lot of different things, there may be a tree of possible commands. So, for instance, in rails, the rails app can generate things for you. So you might do rails generate migration to generate a database migration. Think of chained switches as being analogous to nested controllers in a web application if that helps.
38:39 Arguments
Switches may have their own arguments. The delimiter within a switch tends to be different than the delimiter between switches to make parsing easier. It’s also common to have switches indicating the level/verbosity of output that you want for certain kinds of commands.
40:39 Error Handling
When an app experiences an error, it’s best practice to return an exit code indicating failure. Commandline arguments may or may not be case sensitive, based on the environment. Many commandline apps have commandline switches to do a dry run. In other words, they tell you what would happen if you ran the command with those switches, without actually doing it.
44:05 Other Things To Note
If you are building a GUI application to run on a desktop or server, your app still receives and can use commandline parameters. You can still use stdin, stdout, and stderror in a GUI application.
” In fact, this is an excellent way to figure out what is going wrong in many linux programs when they hang up.”
You can offer commandline autocompletion in some environments. If you have a lot of available parameters, or complex ones, you may wish to offer an options file instead of just commandline parameters.
IoTease: Project
Windows IoT Core Breathalyzer
Even if console apps don’t make you want to drink the Windows IoT Core Breathalyzer is an interesting project. It is an internet connected breathalyzer that writes to the cloud to record trends and predict when you might be at risk for having too much to drive home. It uses an analog sensor with an LCD to prompt users with instructions. The creator of the project points out that this is for novelty purposes not scientific or legal ones.
Hardware
- Raspberry Pi
- Breadboard
- Breadboard Jumper Cables
- MCP3208 Analog to Digital Converter Chip
- MQ2 or MQ3 alcohol Sensor
- SainSmart 1.8 TFT Color Display Module
- External HDMI Screen
Software
- Visual Studio
- Windows IoT Core
- Syncfusion Essential Studio for UWP
Tricks of the Trade
Learn the old tech. The new tech is built on it and you’d be surprised how often you see echoes of the old in the new, whether it is fixes for yesterday’s problems, or problems in new tech that don’t make sense until you understand what it was built on.
Whenever I start a new application, I start out as a console application. That way you can get the logic down before you start adding a flashy UI to it. This process is what one of my old university lecturers called “getting it working before adding the bells and whistles”.
I tend to do a lot of work in .NET Core (in my own time) these days, and pretty much everything you build for that is a console application. It’s a really cool return to form. It really reminds me of when I was a youngster, starting out on my first computer, writing little applications in BASIC and watching them run.
A lot of ops folks (in my experience) tend to live in the TUI (or Text User Interface), and a lot of newer technologies (node, docker, etc.) tend to use the shell and TUI almost exclusively, and I can see why. Once you’re proficient with the terminal/shell/command line, you can do things so much quicker there than when using a GUI application.