Code Monkey home page Code Monkey logo

glint's Introduction

glint

Hex Package Hex.pm Hex Docs GitHub Workflow Status

Gleam command line argument parsing with basic flag support.

Installation

To install from hex:

gleam add glint

Usage

Glint's Core

glint is conceptually quite small, your general flow will be:

  1. create a new glint instance with glint.new
  2. configure it with glint.with_pretty_help and other configuration functions
  3. add commands with glint.add
    1. create a new command with glint.cmd
    2. assign that command any flags required
    3. assign the command a custom description
  4. run your cli with glnt.run, run with a function to handle command output with glint.run_and_handle

✨ Complementary packages

Glint works amazingly with these other packages:

  • argv, use this for cross-platform argument fetching
  • gleescript, use this to generate erlang escripts for your applications

Mini Example

You can import glint as a dependency and use it to build simple command-line applications like the following simplified version of the the hello world example

// stdlib imports
import gleam/io
import gleam/list
import gleam/result
import gleam/string.{uppercase}
// external dep imports
import snag
import argv
// glint imports
import glint
import glint/flag

/// the key for the caps flag
const caps = "caps"

/// a boolean flag with default False to control message capitalization.
///
fn caps_flag() -> flag.FlagBuilder(Bool) {
  flag.bool()
  |> flag.default(False)
  |> flag.description("Capitalize the provided name")
}

/// the command function that will be executed
///
fn hello(input: glint.CommandInput) -> Nil {
  let assert Ok(caps) = flag.get_bool(from: input.flags, for: caps)

  let name =
    case input.args {
        [] -> "Joe"
        [name,..] -> name
    }

  let msg = "Hello, " <> name <> "!"


  case caps {
    True -> uppercase(msg)
    False -> msg
  }
  |> io.println
}

pub fn main() {
  // create a new glint instance
  glint.new()
  // with an app name of "hello", this is used when printing help text
  |> glint.with_name("hello")
  // with pretty help enabled, using the built-in colours
  |> glint.with_pretty_help(glint.default_pretty_help())
  // with a root command that executes the `hello` function
  |> glint.add(
      // add the command to the root
      at: [],
      // create the command, add any flags
      do: glint.command(hello)
      // with flag `caps`
      |> glint.flag(caps, caps_flag())
      // with a short description
      |> glint.description("Prints Hello, <NAME>!"),
  )
  |> glint.run(argv.load().arguments)
}

glint's People

Contributors

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