Giter Club home page Giter Club logo

clj-gflags's Introduction

clj-gflags

Build Status Coverage Status

Google flags ("gflags") for Clojure. See https://github.com/gflags/python-gflags.

The Google approach to command-line flags is that they are defined in the source file in which they are used. This means that in a situation with lots of code sharing, where you may have 3 or 4 command-line apps that all use one common module, the common module can define its own command line flags, which then get used by each app.

It may sound a little weird, but in practice it works well and can be very convenient.

In this translation of the concept to clojure, each namespace can define its own flags.

Installation

[com.lemonodor/gflags "0.7.3"]

Example usage

Say you have a namespace for your S3 utilities. You can define some flags in that namespace for your AWS credentials etc.:

(ns s3-utils
  "Utilties for accessing AWS S3."
  (:require [com.lemonodor.gflags :as gflags])
  ...)

(gflags/define-string "aws-access-key"
  nil
  "The AWS access key to use.")
(gflags/define-string "aws-secret-key"
  nil
  "The AWS secret key to use.")
(gflags/define-integer "num-retries"
  3
  "The number of times to retry S3 operations.")

(defn get-s3-file [url & {:keys [access-key secret-key num-retries]}]
  (let [access-key (or access-key (gflags/flags :aws-access-key))
        secret-key (or secret-key (gflags/flags :aws-secret-key))]
    ...))

Then in a CLI app namespace, you just need to call parse-flags:

(ns main-app
  "Command-line app"
  (:require [com.lemonodor.gflags :as gflags])
  ...)

(defn -main [& args]
  (let [unparsed-args (gflags/parse-flags (into ["argv0"] args))]
    ...))

Status

This code is under development (but can definitely be useful in its current state).

Supported

  • Flag types: define-string, define-boolean, define-integer, define-float, define-enum, define-list, define-multi-string, define-multi-integer, define-multi-float.
  • Long names (:name) and short names (:short-name).
  • Required flags (:required? true)
  • --flagfile
  • Errors if a flag with the same name is registered in two different namespaces.

Not yet

  • --help, --helpshort, --helpxml, --undefok.
  • Flag serialization.
  • Flag validators.
  • "spaceseplist" flags.

Contributing

To run tests:

$ lein test

clj-gflags's People

Contributors

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