Giter VIP home page Giter VIP logo

kotlinx-cli's Introduction

Fork Info

This fork is to enable the consumption of kotlinx.cli via jitpack.io.

How to consume in your gradle build:

  1. Add jitpack.io as a maven repository as described here.
  2. Add the following dependency:
compile 'com.github.kgilmer:kotlinx-cli:bdb0c811'

kotlinx.cli

JetBrains incubator project GitHub license

TODO download link

Pure Kotlin implementation of a generic command-line parser.

  • Declarative: describe what your commands and parameters are
  • Platform-agnostic: core library has no platform-specific dependencies and can be used in any Kotlin project
  • Hackable: build extensions on top of it however you like

kotlinx.cli can be used to create user-friendly and flexible command-line interfaces for Kotlin/JVM, Kotlin/Native, and any other Kotlin console applications. Program defines what arguments are expected. kotlinx.cli will figure out how to parse those, reporting errors if the program arguments are invalid, and also generate help and usage messages as well.

Example

package kotlinx.cli.examples

import kotlinx.cli.*
import kotlin.system.exitProcess

fun main(args: Array<String>) {
    // Define command-line interface 
    val cli = CommandLineInterface("Example1")
    val integers by cli.positionalArgumentsList("N+", "Integers", minArgs = 1)
    val radix by cli.flagValueArgument("-r", "radix", "Input numbers radix", 10) { it.toInt() }
    val sum by cli.flagArgument("--sum", "Print sum")
    val max by cli.flagArgument("--max", "Print max")
    val min by cli.flagArgument("--min", "Print min")

    // Parse arguments or exit
    try {
        cli.parse(args)
    }
    catch (e: Exception) {
        exitProcess(1)
    }

    // Do something useful
    val ints = integers.map { it.toInt(radix) }
    println("Args: ${args.asList()}")
    println("Integers: $ints")
    if (sum) println("Sum: ${ints.sum()}")
    if (max) println("Max: ${ints.max()}")
    if (min) println("Min: ${ints.min()}")
}

Running this program without arguments produces the following output:

Usage: Example1 [-h] N+ [-r radix] [--sum] [--max] [--min] 

-h, --help                Prints help
N+                        Integers
-r radix                  Input numbers radix
--sum                     Print sum
--max                     Print max
--min                     Print min

Now, do some real work: run it with arguments -r 16 CAFE BABE DEAD BEEF --sum

Args: [-r, 16, CAFE, BABE, DEAD, BEEF, --sum]
Integers: [51966, 47806, 57005, 48879]
Sum: 205656

kotlinx-cli's People

Contributors

dnpetrov avatar kgilmer avatar

Stargazers

44 avatar

Watchers

 avatar  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.