Skip to content

Commit 9cd795d

Browse files
committed
Parsing the command line.
1 parent be43f09 commit 9cd795d

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

.github/workflows/run.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ jobs:
1010
steps:
1111
- name: clone
1212
uses: actions/checkout@v3
13-
- name: run
13+
- name: step00
1414
run: (cd step00_hw; ./run.sh)
15+
- name: step01
16+
run: (cd step01_cmdline; ./run.sh)

step00_hw/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
set -e
44

55
docker build . -t demo
6-
docker run -t demo
6+
docker run --rm -t demo

step01_cmdline/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Build the binaries statically, and then run them from a separate container.
2+
FROM alpine AS build
3+
4+
# Intentionally not installing `cargo`, etc. But it does need `clang`.
5+
RUN apk add rustup clang musl
6+
7+
# Init local rust via `rustup`, including static targets builder.
8+
RUN rustup-init -y -t x86_64-unknown-linux-musl --no-modify-path
9+
RUN mv /root/.cargo/bin/* /usr/local/bin/
10+
11+
# Build the project.
12+
COPY ./code /code
13+
WORKDIR /code
14+
RUN cargo build --release --target x86_64-unknown-linux-musl
15+
16+
# Prepare the static binary.
17+
RUN cp ./target/x86_64-unknown-linux-musl/release/add /
18+
19+
# The resulting container with the static binary only.
20+
FROM scratch
21+
COPY --from=build /add /add
22+
ENTRYPOINT ["/add"]

step01_cmdline/code/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "add"
3+
edition = "2021"
4+
5+
[dependencies]
6+
clap = { version = "4.0", features = ["derive"] }

step01_cmdline/code/src/main.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use clap::Parser;
2+
3+
#[derive(Parser)]
4+
struct Args {
5+
#[arg(long)]
6+
a: u32,
7+
#[arg(long)]
8+
b: u32,
9+
}
10+
11+
fn main() {
12+
let args = Args::parse();
13+
println!("{} + {} = {}", args.a, args.b, args.a + args.b);
14+
}

step01_cmdline/run.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
docker build . -t demo
6+
docker run --rm -t demo --a 1 --b 2
7+
docker run --rm -t demo --a 3 --b 4

0 commit comments

Comments
 (0)