In the previous step, we were able to prompt a user for input. This is useful for human input, but more often in AWS we want to automate our workflow, which means we need a method of passing parameters via the Command Line Interface (CLI), for example when using a script
Create a new file called lab_5_step_2_cli_arguments.py and add the following code:
lab_5_step_2_cli_arguments.py
import boto3 def translate_text(**kwargs): client = boto3.client('translate') response = client.translate_text(**kwargs) print(response) ### Change below this line only ### text = input("Provide the text you want translating: ") source_language_code = input("Provide the two letter source language code: ") target_language_code = input("Provide the two letter target language code: ") def main(): translate_text( Text=text, SourceLanguageCode=source_language_code, TargetLanguageCode=target_language_code ) if __name__=="__main__": main()
Python has a built-in argument parser called argparse. It makes it easy to write user-friendly command-line interfaces. There is a lot in the next section, but don’t worry, it should be simple to figure out.
Modify your program to include the code below:
import argparse # argparse is a built in python package, we add it with an import statement import boto3 # Define the parser variable to equal argparse.ArgumentParser() parser = argparse.ArgumentParser(description="Provides translation between one source language and another of the same set of languages.") # Add each of the arguments using the parser.add_argument() method parser.add_argument( '--text', dest="Text", type=str, help="The text to translate. The text string can be a maximum of 5,000 bytes long. Depending on your character set, this may be fewer than 5,000 characters", required=True ) parser.add_argument( '--source-language-code', dest="SourceLanguageCode", type=str, help="The language code for the language of the source text. The language must be a language supported by Amazon Translate.", required=True ) parser.add_argument( '--target-language-code', dest="TargetLanguageCode", type=str, help="The language code requested for the language of the target text. The language must be a language support by Amazon Translate.", required=True ) # This will inspect the command line, convert each argument to the appropriate type and then invoke the appropriate action. args = parser.parse_args() def translate_text(**kwargs): client = boto3.client('translate') response = client.translate_text(**kwargs) print(response) def main(): # vars() is an inbuilt function which returns a dictionary object translate_text(**vars(args)) if __name__=="__main__": main()
To run the program, enter the following command in the terminal:
python lab_5_step_2_cli_arguments.py --text "we are learning python on AWS" --source-language-code en --target-language-code fr
We have added the parameters on the command line.
Each parameter relates to each block for parser.add_argument
parser.add_argument
There is a lot of new code in this example so we will break it down below.
We imported a python package called argparse using import argparse
import argparse
We created an ArgumentParser object which holds the information necessary to parse the command line into python data types
We used parser.add_argument() to add each of the arguments. This has the following syntax
parser.add_argument()
--text
dest
text="value"
Text="value"
type
help
args = parser.parse_args() will inspect the command line, convert each argument to the appropriate type and then invoke the appropriate action
args = parser.parse_args()
Now when we call the translate_text() function we need to use **vars(args). The **vars turns our object created by args = parser.parse_args() into a dictionary object which we can pass as key:value pairs to our function
**vars(args)
**vars
key:value
translate_text()
If you have time try the following:
TerminologyNames
print(args)
aws translate translate-text help