Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jiacai2050/simargs

A simple, opinionated, struct-based argument parser in Zig.
https://github.com/jiacai2050/simargs

argument-parser argument-parsing cli zig zig-library ziglang

Last synced: 13 days ago
JSON representation

A simple, opinionated, struct-based argument parser in Zig.

Awesome Lists containing this project

README

        

#+TITLE: SimArgs
#+DATE: 2022-12-07T22:56:41+0800
#+LASTMOD: 2023-10-21T13:10:50+0800
#+AUTHOR: Jiacai Liu
#+EMAIL: [email protected]
#+OPTIONS: toc:nil num:nil
#+STARTUP: content

#+begin_quote
Notice: this repo has moved to https://github.com/jiacai2050/zigcli.
#+end_quote

[[https://github.com/jiacai2050/simargs/actions/workflows/CI.yml][https://github.com/jiacai2050/simargs/actions/workflows/CI.yml/badge.svg]]

A simple, opinionated, struct-based argument parser in Zig, taking full advantage of [[https://kristoff.it/blog/what-is-zig-comptime/][comptime]].

Blog post explaining how =simargs= is implemented:
- [[https://en.liujiacai.net/2022/12/14/argparser-in-zig/][What I learn by implementing argparser in Zig]]
* Features
- Supported data type:
- All [[https://ziglang.org/documentation/master/#Primitive-Types][primitive types]], such as =i8=, =f32=, =bool=
- =[]const u8=
- =Enum=
- Optional fields and fields with default value mean they are optional arguments
- Use =comptime= as possible as I can
- Provide =print_help()= out of the box
* Usage
See [[file:demo.zig]]

#+begin_src bash :results verbatim :exports both
# Run demo
zig build && ./zig-out/bin/demo -o /tmp/a.out --user-agent Firefox hello world 2>&1
#+end_src

#+RESULTS:
#+begin_example
------------------------------Program------------------------------
./zig-out/bin/demo

------------------------------Arguments------------------------------
verbose: null
user-agent: demo.main__struct_1677.main__struct_1677__enum_1777.Firefox
timeout: 30
output: /tmp/a.out
help: false

------------------------------Positionals------------------------------
1: hello
2: world

------------------------------print_help------------------------------
USAGE:
./zig-out/bin/demo [OPTIONS] [--] [file]

OPTIONS:
-v, --verbose Make the operation more talkative
-A, --user-agent STRING (valid: Chrome|Firefox|Safari)(default: Firefox)
--timeout INTEGER Max time this request can cost(default: 30)
-o, --output STRING Write to file instead of stdout(required)
-h, --help
#+end_example
* Install
First add =simargs= to =build.zig.zon= of your project
#+begin_src zig
.{
.name = "..",
.version = "..",
.dependencies = .{
.simargs = .{
.url = "https://github.com/jiacai2050/simargs/archive/0a1a2afd072cc915009a063075743192fc6b1fd5.tar.gz",
.hash = "1220a6554eccb2e9a9d7d63047e062314851ffd11315b9e6d1b5e06a9dde3275f150",
},
},
}

#+end_src
Then addModule in =build.zig=
#+begin_src zig
const dep_simargs = b.dependency("simargs", .{
.target = target,
.optimize = optimize,
});
exe.addModule("simargs", dep_simargs.module("simargs"));
#+end_src

Finally import like this in your Zig code:
#+begin_src zig
const simargs = @import("simargs");
#+end_src

* Who's Using
- [[https://github.com/jiacai2050/zigcli][jiacai2050/zigcli: Command line programs written in Zig.]]

If you’d like to be added to this list, welcome to [[https://github.com/jiacai2050/simargs/pulls][open a PR]].
* Acknowledgment
When implement =simargs=, I refer following projects to learn how to write
idiomatic Zig code. Big Thanks!
- [[https://github.com/MasterQ32/zig-args/][MasterQ32/zig-args]]
- [[https://github.com/Hejsil/zig-clap][Hejsil/zig-clap]]
- [[https://github.com/evangrayk/zig-argparse][evangrayk/zig-argparse]]