#cmd args parse

1 messages · Page 1 of 1 (latest)

cursive crest
#

I want to use the argparse package in Python, but it's painful to write a mojo code like

from python import (Python, PythonObject)

fn main():
    try:
        let ap = Python.import_module("argparse")
        let parser = ap.ArgumentParser()
        parser.add_argument(...) # this line is painful since key args are not supported yet
        ...
    except e:
        print(e.value)

thus I think it will be more reasonable to implement a simple argparse in mojo....

The question is that is there any existing codes? or would Modular offer official module in the future?

mortal monolith
#

I will do whole things in Python to handle args in python only. Simply.

in main.mojo

fn main():
let args_dict = Python.import_module("my_app").parse()
... do things in mojo.

in my_pp.py:

def parse():
parser = argparse.ArgumentParser():
parser.add_argument(...) # do the easy things in Python.

#

Really, there is no benefit to write simple glue code in Mojo. It wil be painful and useless .

spiral ocean
#

yes, good use!

fathom storm
lean stream
fathom storm
#

Hi @lean stream sure:

from sys import argv

fn main():
    print(argv()[1])

Then you can pass in the argument:

mojo run argvtest.mojo arg1

But it does result in a crash if you don't pass in the arg, I've raised an issue here for that: https://github.com/modularml/mojo/issues/827

lean stream
#

Thanks @fathom storm!

torn estuary