taurus-org / taurus

Moved to https://gitlab.com/taurus-org/taurus
http://taurus-scada.org
43 stars 46 forks source link

How to port taurus.core.util.argparse -based code? #1064

Closed AlejandroAMarin closed 4 years ago

AlejandroAMarin commented 4 years ago

Changed the usage of taurus.core.util.argparse since there was a deprecated warning that recommended using argparse of click. I used argparse, but taurus keeps using the old argparse and raises errors when using the new. Error:

Traceback (most recent call last):
  File "/home/aalonso/Projects/sardana/src/sardana/taurus/qt/qtgui/extra_macroexecutor/sequenceeditor/argparse_snippet.py", line 48, in <module>
    bad()
  File "/home/aalonso/Projects/sardana/src/sardana/taurus/qt/qtgui/extra_macroexecutor/sequenceeditor/argparse_snippet.py", line 24, in bad
    app_version=sardana.Release.version)
  File "/home/aalonso/Projects/taurus/lib/taurus/qt/qtgui/application/taurusapplication.py", line 214, in __init__
    if parser.version is None and app_version:
AttributeError: 'ArgumentParser' object has no attribute 'version'

The changes on the code are these: Old working code:

    from taurus.core.util import argparse

    parser = argparse.get_taurus_parser()
    parser.set_usage("%prog [options]")
    parser.set_description("desc")

    app = TaurusApplication(cmd_line_parser=parser,
                            app_name="sequencer",
                            app_version=sardana.Release.version)

New code that raises error:

    import argparse
    parser = argparse.ArgumentParser(usage="%prog [options]",
                                     description="description")

    app = TaurusApplication(cmd_line_parser=parser,
                            app_name="sequencer",
                            app_version=sardana.Release.version)

I'm asking for advise on how to use argparse in this situation.

Thanks.

cpascual commented 4 years ago

Hi @ByRellex , I am afraid that the TaurusApplication cmd_line_parser API is not adapted to be used with a regular argparse.ArgumentParser (it works with None or with a parser from the deprecated taurus.core.util.argparse module).

Therefore in order to avoid the deprecation warning, I see 2 possibilities:

Apart, I see a couple of improvements that we could do in Taurus to avoid issues like this one in the future: