Book Image

Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

By : Dan Nixon
Book Image

Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

By: Dan Nixon

Overview of this book

Table of Contents (18 chapters)
Getting Started with Python and Raspberry Pi
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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...