Uploaded image for project: 'Cassandra'
  1. Cassandra
  2. CASSANDRA-17914

Argparse module migration as the Python Optparse is deprecated

    XMLWordPrintableJSON

Details

    • Code Clarity
    • Normal
    • All
    • None
    • Hide

      Optparse to Argparse test writeup 

       

      I copied the old optparse code (cqlshmain.py for trunk and cqlsh.py for 4.0 and 4.1) into a new file called optparse_test_main.py and placed it in the same folder as cqlshmain.py. Then I created a copy of the test file (cqlsh.py) in its same folder called optparse_test.py. I am running each argument individually on both files to see if they get the same output. 

       

        Same 
      --version  yes 
      -c  Yes 
      --no-color  Yes 
      --browser  Yes 
      --ssl  Yes 
      -u  Yes 
      -p  Yes 
      -k  Yes 
      -f  Yes 
      --debug  Yes 
      --coverage  Yes 
      --encoding  Yes 
      --cqlshrc  Yes 
      --credentials  Yes 
      --cqlversion  Yes 
      --protocol-version  Yes 
      -e  Yes 
      --connect-timeout  Yes 
      --request-timeout  Yes 
      -t  Yes 
      -v  Yes 
      --insecure-password-without-warning  yes 

       

      Show
      Optparse to Argparse test writeup    I copied the old optparse code (cqlshmain.py for trunk and cqlsh.py for 4.0 and 4.1) into a new file called optparse_test_main.py and placed it in the same folder as cqlshmain.py. Then I created a copy of the test file (cqlsh.py) in its same folder called optparse_test.py. I am running each argument individually on both files to see if they get the same output.      Same  --version  yes  -c  Yes  --no-color  Yes  --browser  Yes  --ssl  Yes  -u  Yes  -p  Yes  -k  Yes  -f  Yes  --debug  Yes  --coverage  Yes  --encoding  Yes  --cqlshrc  Yes  --credentials  Yes  --cqlversion  Yes  --protocol-version  Yes  -e  Yes  --connect-timeout  Yes  --request-timeout  Yes  -t  Yes  -v  Yes  --insecure-password-without-warning  yes   

    Description

      Deprecated since version 2.7: The optparse module is deprecated and will not be developed further; development will continue with the argparse module.

      Argparse is described in PEP 389 – argparse - New Command Line Parsing Module

       

      A partial upgrade path from optparse to argparse:

      https://docs.python.org/3/library/argparse.html#upgrading-optparse-code

      • Replace (options, args) = parser.parse_args() with args = parser.parse_args() and add additional ArgumentParser.add_argument() calls for the positional arguments. Keep in mind that what was previously called options, now in the argparse context is called args.
      • Replace callback actions and the callback_* keyword arguments with type or action arguments.
      • Replace string names for type keyword arguments with the corresponding type objects (e.g. int, float, complex, etc).
      • Replace optparse.Values with Namespace and optparse.OptionError and optparse.OptionValueError with ArgumentError.
      • Replace strings with implicit arguments such as %default or %prog with the standard Python syntax to use dictionaries to format strings, that is, %(default)s and %(prog)s.
      • Replace the OptionParser constructor version argument with a call to parser.add_argument('--version', action='version', version='<the version>').

      Attachments

        Activity

          People

            vgali7 Vineet Gali
            bschoeni Brad Schoening
            Vineet Gali
            Brandon Williams, Stefan Miklosovic
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: