cmd args parse
I want to use the
argparse
package in Python, but it's painful to write a mojo code like
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?9 Replies
yes there are thoses features and if you want, you can pass -D key=value to mojo
https://docs.modular.com/mojo/stdlib/os/env.html https://docs.modular.com/mojo/stdlib/sys/arg.html https://docs.modular.com/mojo/stdlib/sys/param_env.html
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 .
yes, good use!
Mojo has this: https://docs.modular.com/mojo/stdlib/sys/arg.html
Keyword arguments are being worked on currently
Hey @Jack Clayton, could you give an example on how to use this function argv? Mojo is throwing a errors when I am trying it in a pythonic fashion
Hi @bhavnicksm sure:
Then you can pass in the argument:
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
Thanks @Jack Clayton!
would have to check length of argv() before accessing argv()[1], right? I mean, to avoid the crash, you've mentioned
actually - now it no longer crashes but prints "(null)"
Yeah should do that