Parsing command line parameters and using that data to control your program

Parsing command line parameters and using that data to control your program

·

3 min read

In the previous article I showed how you can include and check input parameters in Python. But all we did was check for the existence of the parameter, we didn't capture any data associated with it.

ERROR:  Developer forgot about capturing the message text!

Using the argparse.ArgumentParser, we need to add arguments describing the nature of the data we are trying to capture from the command line.

Here you will note that we want to use a switch "-s" or "--send" to indicate that we are sending a message. The piece of data after that switch will be the message we want to send.

parser.add_argument('-s', '--send',    type=str,   nargs='?', help="send a text message")

We indicate the data is string, something about a NARG. Not sure what that exactly means but you need it to capture the data.

Later on in the code we take the input parameter data and assign it to a local variable:

#process arguments and assign values to local variables
if(args.send):
  SendMessage = True
  TheMessage = args.send
else:
  SendMessage = False

All the parameters combined look like this:

NAME = 'MeshTalk'                   
DESCRIPTION = "Send and recieve messages to a MeshTastic device"
DEBUG = False

parser = argparse.ArgumentParser(description=DESCRIPTION)
parser.add_argument('-s', '--send',    type=str,   nargs='?', help="send a text message")
parser.add_argument('-r', '--receive', action='store_true',   help="recieve and display messages")
parser.add_argument('-t', '--time',    type=float, nargs='?', help="seconds to listen before exiting",default = 60)
args = parser.parse_args()


#process arguments and assign values to local variables
if(args.send):
  SendMessage = True
  TheMessage = args.send
else:
  SendMessage = False

if(args.receive):
  ReceiveMessages = True
else:
  ReceiveMessages = False

TimeToSleep = args.time

Using the parameters to control logic

Now that we have the data loaded (in the case of sending a message) or we have decided to simply receive and decode messages, we can go ahead and make the necessary code changes.

#Check for message to be sent
if(SendMessage):
  print("Sending: ",TheMessage)
  interface.sendText(TheMessage)
  print("Message sent")

if(ReceiveMessages):
  print("Listening for:",TimeToSleep," seconds")
  print("Subscribing to interface channels...")
  pub.subscribe(onReceive, "meshtastic.receive")
  time.sleep(TimeToSleep)


interface.close()  
print ("--End of Line------------")
print ("")

Receiving Messages

And here is what it looks like when we run it from the Raspberry Pi command shell. In this example I am telling the Meshtastic device to list all incoming messages for the default of 60 seconds.

2021-09-09 18_01_58-pi@meshtasticPi_ ~_meshtastic_meshtalk.jpg

Sending a message

Now the part we have all been waiting for. Sending a message from python to a LORA device.

2021-09-09 18_17_35-pi@meshtasticPi_ ~_meshtastic_meshtalk.jpg

And this is what it looks like in the Meshtastic Android app:

2021-09-09 18_28_30-C__Users_Bill_Documents_AirDroid_PublicStaging_Screenshot_20210909-181824_Meshta.jpg

I am super pumped. This will be put up into GitHub now and shared to the world. Hopefully that one dude will find it and learn how to expand on it.