Reader small image

You're reading from  Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

Product typeBook
Published inSep 2015
Reading LevelBeginner
Publisher
ISBN-139781783551590
Edition1st Edition
Languages
Right arrow
Author (1)
Dan Nixon
Dan Nixon
author image
Dan Nixon

Dan Nixon is a software and electronics engineer living in the north of England. He has past experience of creating software for data analysis, process control, and business intelligence applications. In most of these projects, Python was one of the main languages used. Dan previously authored another book on the uses of the Raspberry Pi, called Raspberry Pi Blueprints, and has worked on many personal projects that use both Python and the Raspberry Pi.
Read more about Dan Nixon

Right arrow

Chapter 9. Creating Command-line Interfaces

In this chapter, we will take a look at how we can use the argparse module to create easy to use command line interfaces (CLIs). CLIs can provide a much easier way to develop interface for your applications and are appropriate for situations where a graphical interface is not necessarily required.

To demonstrate the use of the module, we will build a simple unit conversion application that allows the user to input a value in a given unit and have the program convert it to a set of different units.

Unit conversion application


Before we can design the command line interface, we need to build up the framework for the unit conversion application. A rough structure of this application is shown in the following UML diagram:

Note

Note that because the unit conversion portion of this application is not the main focus here, not all the code for the unit conversions will be listed. However, they are available with the code download for this chapter.

The UnitTable class contains functions that convert between the units by first converting to a common base unit. To do this, there are two dictionaries with conversion steps for each unit supported by the table: one to convert to from the base unit and one to convert to the base unit.

Additional tables are created by inheriting from the UnitTable class and adding new entries to the to_base_unit and from_base_unit dictionaries. Note that every unit in a unit table must be able to convert to and from the base unit.

We will start by using the existing unit...

Command-line interface


Now that the framework for the unit conversion is complete, we can start work on the command line interface, used to form the full application.

We will expand the application to include a command line interface by creating a new file named CLI.py in the unitconverter directory. We will start this file by including the required modules and functions:

import argparse
import inspect
from Converter import get_table, convert_units

The run_cli() function is going to be called whenever the unit conversion application is invoked from the command line. It is responsible for parsing the input from the command line and performing the required conversions. This starts by creating a ArgumentParser object, to which we can add arguments that are to be parsed from the command line:

def run_cli():
    parser = argparse.ArgumentParser(description='Tool for converting units')

The first argument will be used to define the conversion table that will be used in the unit conversion:

    parser...

Summary


In this chapter, we looked at how the argparse Python module can be used to create command line interfaces around applications.

In the next chapter, we will look at the ways in which we can troubleshoot the Python code we write, and design applications to be easily debugged by having them write log files.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)
Published in: Sep 2015Publisher: ISBN-13: 9781783551590
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Dan Nixon

Dan Nixon is a software and electronics engineer living in the north of England. He has past experience of creating software for data analysis, process control, and business intelligence applications. In most of these projects, Python was one of the main languages used. Dan previously authored another book on the uses of the Raspberry Pi, called Raspberry Pi Blueprints, and has worked on many personal projects that use both Python and the Raspberry Pi.
Read more about Dan Nixon