1

I am using the Alexa Gadget Toolkit Python package to create an IoT device. (https://github.com/alexa/Alexa-Gadgets-Raspberry-Pi-Samples/tree/master/src/agt)

Following from Amazon's example scripts, the IoT class is defined and executed as:

from agt import AlexaGadget

class MyGadget(AlexaGadget):
    ...

if __name__ == "__main__":
    MyGadget().main()

I'd like to add an additional "test" argument to my gadget script (to enter a testing mode), so I am using argparser.

import argparse
from agt import AlexaGadget

class MyGadget(AlexaGadget):
    def main():
        super().main()
        
        parser = argparse.ArgumentParser()
        parser.add_argument("--test", action="store_true", required=False)
        args = parser.parse_args()

        if args.test:
            # Do something here

However, when I do this, reagardless of whether or not I call the super().main() before or after defining parser, I receive:

usage: mygadget.py [-h] [--pair] [--clear]
mygadget.py: error: unrecognized arguments: --test

Looking at the AGT class, they also use argparser to define arguments related to the Bluetooth functionality.

Since it is already defined in the parent, how can I also capture my own arguments in my child without modifying Amazon's AGT class?

Thanks!

Jamie Poole
  • 508
  • 6
  • 17
  • 3
    Each parser runs independently looking at the same `sys.argv` commandline list. While your own parser can handle extras with `parse_unknown_args` you can't do anything with the other. I wonder if there's a point where you can remove the `--test` argument from `sys.argv` before AGT looks at it. – hpaulj Dec 07 '20 at 17:41
  • Thanks right yeah I will look into that method. I will add a comment to the Alexa GitHub to see if they can change their class. – Jamie Poole Dec 07 '20 at 18:16
  • With some of the Google tools (youtube access etc), it's possible call them in a non-script manner that doesn't run the parser. Other packages like `ipython` let you supply your own arguments after a delimiter like '--'. – hpaulj Dec 07 '20 at 18:41

0 Answers0