Code Monkey home page Code Monkey logo

yazap's Introduction

yazap

yazap is a command line argument parser for zig which provides simple and easy to use API for you to define custom Argument, flag, subcommand and nested subcommand.

Inspired from clap-rs and andrewrk/ziglang: src-self-hosted/arg.zig

Features

  • Flags

    • boolean/no argument flag (-f, --flag)

    • single argument flag (-f, --flag <VALUE>)

    • multi argument flag (-f, --flag <VALUES>)

      Note: You have to explicitly set the number of arguments for it

    • single argument flag with options (-f, --flag <A | B | C>)

    • Support passing value using space -f value no space -fvalue and using = (-f=value)

    • Support chaining multiple short flags

      • -xy where both x and y does not take value
      • -xyz=value
    • Support for specifying flag multiple times (-x a -x b -x c)

  • Subcommand

    • app bool-cmd
    • app single-arg-cmd <ARG>
    • app multi-arg-cmd <ARG1> <ARG2> <ARGS3...>
    • app flag-cmd [flags]
    • app arg-and-flag-cmd <ARG> [FLAGS]
    • Nested subcommand
  • Defining custom Argument

Installation Guide

Before you follow below steps be sure to initialize your project as repo by running git init

  1. On your root project make a directory named libs
  2. Run git submodule add https://github.com/PrajwalCH/yazap libs/yazap
  3. After above step is complete add the following code snippet on your build.zig file
    exe.addPackagePath("yazap", "libs/yazap/src/lib.zig");
  4. Now you can import this library on your src file as
    const yazap = @import("yazap");

Docs

Please visit here for documentation reference

Example

Checkout examples/ for more.

const std = @import("std");
const yazap = @import("yazap");

const allocator = std.heap.page_allocator;
const log = std.log;
const Command = yazap.Command;
const flag = yazap.flag;

pub fn main() anyerror!void {
    var ls = Command.new(allocator, "ls");
    defer ls.deinit();

    try ls.addArg(flag.boolean("all", 'a'));
    try ls.addArg(flag.boolean("recursive", 'R'));
    // For now short name can be null but not long name
    // that's why one-line long name is used for -1 short name
    try ls.addArg(flag.boolean("one-line", '1'));
    try ls.addArg(flag.boolean("size", 's'));
    try ls.addArg(flag.boolean("version", null));
    try ls.addArg(flag.boolean("help", null));

    try ls.addArg(flag.argOne("ignore", 'I'));
    try ls.addArg(flag.argOne("hide", null));

    try ls.addArg(flag.option("color", 'C', &[_][]const u8{
        "always",
        "auto",
        "never",
    }));

    var ls_args = try ls.parseProcess();
    defer ls_args.deinit();

    // It's upto you how you check for each args
    // for now i am showing you in a straightforward way

    if (ls_args.isPresent("help")) {
       log.info("show help", .{});
       return;
    }

    if (ls_args.isPresent("version")) {
        log.info("v0.1.0", .{});
        return;
    }

    if (ls_args.isPresent("all")) {
        log.info("show all");
        return;
    }

    if (ls_args.isPresent("recursive")) {
        log.info("show recursive", .{});
        return;
    }

    if (ls_args.valueOf("ignore")) |pattern| {
        log.info("ignore pattern = {s}", .{pattern});
        return;
    }

    if (ls_args.isPresent("color")) {
        const when = ls_args.valueOf("color").?;

        log.info("color={s}", .{when});
        return;
    }
}

Alternate Parsers

yazap's People

Contributors

signaryk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.