create/
create.rs

1//! Creates an archive from one or more input files.
2//!
3//! To create a new archive, run:
4//!
5//! ```shell
6//! cargo run --example create <path/to/output.a> <path/to/input1> <input2..>
7//! ```
8//!
9//! Assuming the output file doesn't already exist, this is roughly equivalent
10//! to running:
11//!
12//! ```shell
13//! ar -cr <path/to/output.a> <path/to/input1> <input2..>
14//! ```
15
16extern crate ar;
17
18use std::env;
19use std::fs::File;
20use std::path::Path;
21
22fn main() {
23    let num_args = env::args().count();
24    if num_args < 3 {
25        println!("Usage: create <outpath> <inpath> [<inpath>...]");
26        return;
27    }
28
29    let output_path = env::args().nth(1).unwrap();
30    let output_path = Path::new(&output_path);
31    let output_file =
32        File::create(output_path).expect("failed to open output file");
33    let mut builder = ar::Builder::new(output_file);
34
35    for index in 2..num_args {
36        let input_path = env::args().nth(index).unwrap();
37        let input_path = Path::new(&input_path);
38        builder
39            .append_path(input_path)
40            .expect(&format!("failed to add {:?} to archive", input_path));
41    }
42}