IT story

명령 행 매개 변수에 액세스하는 방법?

hot-time 2020. 6. 15. 08:10
반응형

명령 행 매개 변수에 액세스하는 방법?


녹 튜토리얼은 명령 줄에서 매개 변수를 사용하는 방법에 대해 설명하지 않습니다. fn main()모든 예제에서 빈 매개 변수 목록으로 만 표시됩니다.

에서 명령 줄 매개 변수에 액세스하는 올바른 방법은 무엇입니까 main?


std::env::args또는 std::env::args_os함수 를 사용하여 명령 줄 인수에 액세스 할 수 있습니다 . 두 함수 모두 인수에 대해 반복자를 반환합니다. 전자는 Strings (작업하기 쉬운)를 반복 하지만 인수 중 하나가 유효한 유니 코드가 아닌 경우 패닉 상태입니다. 후자는 OsStrings를 반복 하고 결코 당황하지 않습니다.

반복자의 첫 번째 요소는 프로그램 자체의 이름 (모든 주요 OS에서 사용되는 규칙)이므로 첫 번째 인수는 실제로 두 번째 반복 된 요소입니다.

결과를 처리하는 쉬운 방법은 다음과 같이 args변환하는 것입니다 Vec.

use std::env;

fn main() {
    let args: Vec<_> = env::args().collect();
    if args.len() > 1 {
        println!("The first argument is {}", args[1]);
    }
}

전체 표준 반복기 도구 상자 를 사용하여 이러한 인수로 작업 할 수 있습니다 . 예를 들어 첫 번째 인수 만 검색하려면 다음을 수행하십시오.

use std::env;

fn main() {
    if let Some(arg1) = env::args().nth(1) {
        println!("The first argument is {}", arg1);
    }
}

crates.io 에서 명령 줄 인수를 구문 분석하기위한 라이브러리를 찾을 수 있습니다 .

  • docopt : 도움말 메시지를 작성하면 구문 분석 코드가 생성됩니다.
  • 박수 : 유창한 API를 사용하여 구문 분석하려는 옵션을 설명합니다. docopt보다 빠르며 더 많은 제어 기능을 제공합니다.
  • getopts : 널리 사용되는 C 라이브러리의 포트 더 낮은 수준의 제어 기능
  • structopt : 박수 위에 제작되어 사용하기가 더 인체 공학적입니다.

Rust에서도 Docopt 를 사용할 수 있으며, 이는 사용 문자열에서 구문 분석기를 생성합니다. Rust의 보너스로 매크로를 사용하여 구조체를 자동으로 생성하고 형식 기반 디코딩을 수행 할 수 있습니다.

docopt!(Args, "
Usage: cp [-a] SOURCE DEST
       cp [-a] SOURCE... DIR

Options:
    -a, --archive  Copy everything.
")

그리고 당신은 다음과 같이 인수를 얻을 수 있습니다 :

let args: Args = Args::docopt().decode().unwrap_or_else(|e| e.exit());

README와 문서 에는 전체 실무 예제가 많이 있습니다.

면책 조항 : 나는이 도서관의 저자 중 하나입니다.


Rust는 getopts crategetopt 에서 스타일 CLI 인수 구문 분석을 수행합니다 .


저에게는 getopts가 항상 너무 낮게 느껴졌고 docopt.rs는 너무 많은 마법이었습니다. 필요한 경우 여전히 모든 기능을 제공하는 명시적이고 간단한 것을 원합니다.

이것은 clap-rs 가 유용한 곳입니다.
파이썬의 argparse와 비슷합니다. 다음은 그 모습에 대한 예입니다.

let matches = App::new("myapp")
                      .version("1.0")
                      .author("Kevin K. <kbknapp@gmail.com>")
                      .about("Does awesome things")
                      .arg(Arg::with_name("CONFIG")
                           .short("c")
                           .long("config")
                           .help("Sets a custom config file")
                           .takes_value(true))
                      .arg(Arg::with_name("INPUT")
                           .help("Sets the input file to use")
                           .required(true)
                           .index(1))
                      .arg(Arg::with_name("debug")
                           .short("d")
                           .multiple(true)
                           .help("Sets the level of debugging information"))
                      .get_matches();

You can access your parameters like so:

println!("Using input file: {}", matches.value_of("INPUT").unwrap());

// Gets a value for config if supplied by user, or defaults to "default.conf"
let config = matches.value_of("CONFIG").unwrap_or("default.conf");
println!("Value for config: {}", config);

(Copied from the official documentation)


As of version 0.8/0.9, the correct path to the function args() would be ::std::os::args, ie:

fn main() {
  let args: ~[~str] = ::std::os::args();
  println(args[0]);
}

It seems that Rust is still pretty volatile right now with even standard IO, so this may become out of date fairly quickly.


Rust changed again. os::args() is deprecated in favor of std::args(). But std::args() is not an array, it returns an iterator. You can iterate over the command line arguments, but cannot access them with subscripts.

http://doc.rust-lang.org/std/env/fn.args.html

If you want the command line arguments as a vector of strings, this will work now:

use std::env;
...
let args: Vec<String> = env::args().map(|s| s.into_string().unwrap()).collect();

Rust - learn to embrace the pain of change.


what @barjak said works for strings, but if you need the argument as a number (in this case a uint) you need to convert like this:

fn main() {
    let arg : ~[~str] = os::args();
    match uint::from_str(arg[1]){
         Some(x)=>io::println(fmt!("%u",someFunction(x))),
         None=>io::println("I need a real number")
    }
}

Also check out structopt:

extern crate structopt;
#[macro_use]
extern crate structopt_derive;

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[structopt(name = "example", about = "An example of StructOpt usage.")]
struct Opt {
    /// A flag, true if used in the command line.
    #[structopt(short = "d", long = "debug", help = "Activate debug mode")]
    debug: bool,

    /// An argument of type float, with a default value.
    #[structopt(short = "s", long = "speed", help = "Set speed", default_value = "42")]
    speed: f64,

    /// Needed parameter, the first on the command line.
    #[structopt(help = "Input file")]
    input: String,

    /// An optional parameter, will be `None` if not present on the
    /// command line.
    #[structopt(help = "Output file, stdout if not present")]
    output: Option<String>,
}

fn main() {
    let opt = Opt::from_args();
    println!("{:?}", opt);
}

https://github.com/TeXitoi/structopt


As of newer Rust versions (Rust > 0.10/11) the array syntax wont work. You will haveto use the get method.

[Edit] The array syntax works (again) in the nightly. So you can choose between the getter or array index.

use std::os;

fn main() {
  let args = os::args();
  println!("{}", args.get(1));
}

// Compile
 rustc args.rs && ./args hello-world // returns hello-world

Rust has evolved since Calvin's answer from May 2013. Now one would parse command line arguments with as_slice():

use std::os;

fn seen_arg(x: uint)
{       
    println!("you passed me {}", x);
}
fn main() {
    let args = os::args();
    let args = args.as_slice();
    let nitems = {
            if args.len() == 2 {
                    from_str::<uint>(args[1].as_slice()).unwrap()
            } else {
                    10000
            }
    };

    seen_arg(nitems);
}

The Rust book "No stdlib" chapter covers how to access the command lines parameters (another way).

// Entry point for this program
#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
    0
}

Now, the example does also have #![no_std] which I think means that normally, the std library would have the true entry point for your binary and call a global function called main(). Another options is to 'disable the main shim' with #![no_main]. Which if I'm not mistaken is saying to the compiler that you are taking full control over how your program is started.

#![no_std]
#![no_main]

#[no_mangle] // ensure that this symbol is called `main` in the output
pub extern fn main(argc: isize, argv: *const *const u8) -> isize {
    0
}

I do not think this is a 'good' way of doing things if all you want to do is read command line arguments. The std::os module mentioned in other answers seems to be a much better way of doing things. I post this answer for the sake of completion.

참고URL : https://stackoverflow.com/questions/15619320/how-to-access-command-line-parameters

반응형