list index out of range
test.py
import argparse
class Test(object):
def __init__(self):
print("Test---------\n")
self.argparser = argparse.ArgumentParser()
test.codon
from python import test
if __name__ == "__main__":
test.Test()
I have the demo above, and run it with codon, and i get the error PyError: list index out of range
$ codon run test.codon
Test---------
PyError: list index out of range
Raised from: pyobj.exc_check:0
/data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:623:13
Backtrace:
[0x7fc1ad3e4af3] GCC_except_table171 at /data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:623:13
[0x7fc1ad3e4b20] GCC_except_table171 at /data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:626:9
[0x7fc1ad3e5688] GCC_except_table171 at /data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:650:68
[0x7fc1ad3e6915] GCC_except_table171 at /workspace/codon.py:14:5
Aborted (core dumped)
It may be because of sys.argv in argparse.ArgumentParser.__init__().
test.py
import sys
class Test(object):
def __init__(self):
print("python: ", sys.argv)
test.codon
from python import test
import sys
if __name__ == "__main__":
test.Test()
print("codon: ", sys.argv)
$ codon run test.codon
python: []
codon: ['hades.codon']
How can i use codon to call python module argparse.ArgumentParser?
Hi @cyw3 -- here's an example of how this can be done (I just took the simple example from the argparse docs):
# .codon file
import sys
from python import argparse
parser = argparse.ArgumentParser(
prog = 'ProgramName',
description = 'What the program does',
epilog = 'Text at the bottom of help')
parser.add_argument('filename') # positional argument
parser.add_argument('-c', '--count') # option that takes a value
parser.add_argument('-v', '--verbose',
action='store_true') # on/off flag
args = parser.parse_args(sys.argv[1:])
print(args.filename, args.count, args.verbose)
Just remember to set the CODON_PYTHON env variable to the Python library as described here.
Hi @cyw3 -- here's an example of how this can be done (I just took the simple example from the
argparsedocs):# .codon file import sys from python import argparse parser = argparse.ArgumentParser( prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help') parser.add_argument('filename') # positional argument parser.add_argument('-c', '--count') # option that takes a value parser.add_argument('-v', '--verbose', action='store_true') # on/off flag args = parser.parse_args(sys.argv[1:]) print(args.filename, args.count, args.verbose)Just remember to set the
CODON_PYTHONenv variable to the Python library as described here.
When I run the demo you provided, I get the error PyError: 0:
$ codon run test.codon
usage: ProgramName [-h] [-c COUNT] [-v] filename
What the program does
positional arguments:
filename
optional arguments:
-h, --help show this help message and exit
-c COUNT, --count COUNT
-v, --verbose
Text at the bottom of help
PyError: 0
Raised from: pyobj.exc_check:0
/data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:623:13
Backtrace:
[0x7f0dff8c3f03] GCC_except_table255 at /data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:623:13
[0x7f0dff8c3f30] GCC_except_table255 at /data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:626:9
[0x7f0dff8c6192] GCC_except_table255 at /data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:650:68
[0x7f0dff8c782c] GCC_except_table255 at /workspace/test.codon:30:8
Aborted (core dumped)
It happens because filename is not specified.
This works:
codon run argparse_python.codon -c 5 -v blabla.txt
['-c', '5', '-v', 'blabla.txt']
Closing this for now but please let me know if you have any other questions about this.