Coder Social home page Coder Social logo

awk-parseopts-util's Introduction

Command line option parser

Description

โš ๏ธ This project is no longer maintained.

Tool to parse command-line options.

Requirenments

  • GNU Awk 5.0.1

Options

No options are supported now.

Option specification

Option specification is command-line argument descriptions bundled together in some code place. It has the following syntax:

option { [-t|--type=integer|float|bool|string] [-a|--alias=<comma-separated-list>] [-ia|--is-assignable=true|false] [-ab|--allow-bundle=true|false] [-ac|--assignment-char=<char>] }

where:

  • option - main command-line option name to be described within curly braces
    • -t|--type - option value type one of integer|float|bool|string (required when -ia|--is-assignable equals to true)
    • -a|--alias - option aliases
    • -ia|--is-assignable - specifies whether option accepts value
    • -ab|--allow-bundle - specifies whether option acceptable value can be assigned via delimiting option and it via -ac|--assignment-char (required when -ia|--is-assignable equals to true)
    • -ac|--assignment-char - assignment char used to specify option value (required when -ia|--is-assignable and -ab|--allow-bundle equal to true)

Example:

-h { --alias=--help --is-assignable=false } -v { --alias=--version --is-assignable=false }

This option specification means that -h|--help and -v|--version options doesn't accept any values.

You can create option specifications via arrays:

options[0] = "-h"
options[1] = "{"
options[2] = "--alias=--help"
options[3] = "--is-assignable=false"
options[4] = "}"

options[5] = "-v"
options[6] = "{"
options[7] = "--alias=--version"
options[8] = "--is-assignable=false"
options[9] = "}"

Or you can use parseoptsrunner.awk to pass arguments and their specifications simplier:

awk -f parseoptsrunner.awk -- -h :: -h '{' --alias=--help --is-assignable=false '}' -v '{' --alias=--version --is-assignable=false '}'

Everything before double colon is argument and everything after is specification.

Examples

Checking whether user-written arguments (arguments array) conforms described option specification (options array):

@include "parseopts.awk"
@include "utils.awk"
@include "colors.awk"

function shiftItems(from, to, source, target) {
  i = from
  j = to
  while (i <= from + length(source) - 1) {
    target[j] = source[i]
    i++
    j++
  }
}

BEGIN {
  split("-f { -a=--first -ia=true -t=bool -ab=true -ac=: } -s { -a=--second -ia=true -t=bool -ab=true -ac=: }", __options, " ")
  shiftItems(1, 0, __options, options)
  utils::clearArray(__options)

  printf "options = "
  utils::printlnArray(options)

  split("--first false -s true", __arguments, " ")
  shiftItems(1, 0, __arguments, arguments)
  utils::clearArray(__arguments)

  printf "arguments = "
  utils::printlnArray(arguments, ", ")

  print parseopts::checkArguments(arguments, options)
}

awk-parseopts-util's People

Stargazers

 avatar

Watchers

 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.