Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using :args to pass command line arguments to the main function #11

Open
phtrivier opened this issue May 3, 2023 · 0 comments
Open

Comments

@phtrivier
Copy link

phtrivier commented May 3, 2023

I have this org file:

:PROPERTIES:
:header-args:rust: :noweb yes :comments noweb
:END:

* The program
** General structure

#+name: main
#+begin_src rust :tangle main.rs
<<imports>>
fn main() {
    <<gather-input>>
    <<count>>
}
<<tests>>
#+end_src

#+RESULTS: main
: thread 'main' panicked at 'No argument provided', src/main.rs:9:9

** Gathering input

We simply get the first parameter, panicking if there is nothing specified.

#+name: gather-input
#+begin_src rust
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
    panic!("No argument provided");
}
<<parse-input>>
#+end_src

... skipped ... 

I can run org-babel-tangle to produce a main.rs file, and things work fine when I run cargo run 3, for example.

However, I would like to provide a command line arguments to the source block, so that I can run org-babel-execute-maybe inside this block, and use a value:

#+name: main
#+begin_src rust :tangle main.rs
<<imports>>
fn main() {
    <<gather-input>>
    <<count>>
}
<<tests>>
#+end_src

I understand that I could use a default argument, etc... However, the doc in ob-rust.el says that args should work, but I could not make it work. Is there something else to do than that ?

#+name: main
#+begin_src` rust :tangle main.rs :args 3
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant