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