• Stars
    star
    130
  • Rank 277,575 (Top 6 %)
  • Language
    Scala
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rapidly build powerful command-line applications powered by ZIO

ZIO CLI

Rapidly build powerful command-line applications powered by ZIO

Experimental CI Badge Sonatype Releases Sonatype Snapshots javadoc ZIO CLI

Installation

To use ZIO CLI, we need to add the following to our build.sbt file:

libraryDependencies += "dev.zio" %% "zio-cli" % "0.6.0"

Getting Started

ZIO CLI allows to easily construct a CLI application. A CLI or Command-Line Interface is an application that allows the user to give instructions by means of pieces of text called commands. A command has the following structure

command param1 param2 ... paramN

where command is the name of the command and param1, param2, ..., paramN form a list of parameters depending on the command that determines the precise instruction to the CLI application.

Given the case, a command itself might contain subcommands. This allows a better design of the command-line application and a more comfortable user experience.

A command might include arguments and options.

  • Arguments are name-less and position-based parameters that the user specifies just by the position in the command. As an example, we can consider the widely used command-line application Git. One subcommand is clone. It creates a copy of an existing repository. An argument of git clone is repository. If the repository name is https://github.com/zio/zio-cli.git, we will use it in the following manner:
git clone https://github.com/zio/zio-cli.git
  • Options are named and position-independent parameters that are specified by writing the content after the name. The name is preceded by --. An option may have a shorter form called an alias. When the alias is used instead of the full name, only - is needed. An option of command git clone is local. It is a boolean option, so it is not necessary to write true or false after it: it will be true only if it appears. It is used in the following manner:
git clone --local

It also has an alias -l:

git clone -l

The description of the command git clone, taking only into account option local and argument repository will be

git clone [-l] <repository>

where [] implies that the option is optional and <> indicates an argument.

Difference between Args and Options

Arguments and options are different due to the way the user specifies them. Arguments are not specified using its name, only by the position inside the command. On the other hand, options must be preceded by its name and -- indicating that it is the name of an option.

Furthermore, a command-line application will represent them in different ways. Argument's name will be inside <> while an option will be preceded by --. In case that the option has a short form or alias, this will be preceded by -.

First ZIO CLI example

This is done by defining cliApp value from ZIOCliDefault using CliApp.make and specifying a Command as parameter. A Command[Model] is a description of the commands of a CLI application that allows to specify which commands are valid and how to transform the input into an instance of Model. Then it is possible to implement the logic of the CLI application in terms of Model. As a sample we are going to create a command of Git. We are going to implement only command git clone with argument repository and option local.

import zio.cli._
import zio.cli.HelpDoc.Span.text
import zio.Console.printLine

// object of your app must extend ZIOCliDefault
object Sample extends ZIOCliDefault {

  /**
   * First we define the commands of the Cli. To do that we need:
   *    - Create command options
   *    - Create command arguments
   *    - Create help (HelpDoc) 
   */
  val options: Options[Boolean] = Options.boolean("local").alias("l")
  val arguments: Args[String] = Args.text("repository")
  val help: HelpDoc = HelpDoc.p("Creates a copy of an existing repository")
  
  val command: Command[(Boolean, String)] = Command("clone").subcommands(Command("clone", options, arguments).withHelp(help))
  
  // Define val cliApp using CliApp.make
  val cliApp = CliApp.make(
    name = "Sample Git",
    version = "1.1.0",
    summary = text("Sample implementation of git clone"),
    command = command
  ) {
    // Implement logic of CliApp
    case _ => printLine("executing git clone")
  }
}

The output will be

   _____@       @           @        @   __@     @       @  ______@   _ @  __ @
  / ___/@ ____ _@  ____ ___ @   ____ @  / /@ ___ @       @ / ____/@  (_)@ / /_@
  \__ \ @/ __ `/@ / __ `__ \@  / __ \@ / / @/ _ \@       @/ / __  @ / / @/ __/@
 ___/ / / /_/ / @/ / / / / /@ / /_/ /@/ /  /  __/@       / /_/ /  @/ /  / /_  @
/____/  \__,_/  /_/ /_/ /_/ @/ .___/ /_/   \___/ @       \____/   /_/   \__/  @
        @       @           /_/      @     @     @       @        @     @     @


Sample Git v1.1.0 -- Sample implementation of git clone

USAGE

  $ clone clone [(-l, --local)] <repository>

COMMANDS

  clone [(-l, --local)] <repository>  Creates a copy of an existing repository

If there is a CliApp, you can run a command using its method run and passing parameters in a List[String].

References

Documentation

Learn more on the ZIO CLI homepage!

Contributing

For the general guidelines, see ZIO contributor's guide.

Code of Conduct

See the Code of Conduct

Support

Come chat with us on Badge-Discord.

License

License

More Repositories

1

zio

ZIO β€” A type-safe, composable library for async and concurrent programming in Scala
Scala
4,077
star
2

zio-quill

Compile-time Language Integrated Queries for Scala
Scala
2,153
star
3

zio-http

A next-generation Scala framework for building scalable, correct, and efficient HTTP clients and servers
Scala
785
star
4

zio-prelude

A lightweight, distinctly Scala take on functional abstractions, with tight ZIO integration
Scala
451
star
5

zio-json

Fast, secure JSON library with tight ZIO integration.
Scala
406
star
6

zio-kafka

A Kafka client for ZIO and ZIO Streams
Scala
336
star
7

zio-actors

A high-performance, purely-functional library for building, composing, and supervising typed actors based on ZIO
Scala
268
star
8

zio-sql

Type-safe, composable SQL for ZIO applications
Scala
236
star
9

zio-config

Easily use and document any config from anywhere in ZIO apps
Scala
231
star
10

zio-intellij

A companion IntelliJ IDEA plugin for the ZIO library ecosystem.
Scala
212
star
11

zio-protoquill

Quill for Scala 3
Scala
205
star
12

zio-keeper

A ZIO library for building distributed systems
Scala
199
star
13

zio-nio

A small, unopinionated ZIO interface to NIO.
Scala
187
star
14

zio-logging

Powerful logging for ZIO 2.0 applications, with compatibility with many logging backends out-of-the-box.
Scala
174
star
15

zio-akka-cluster

ZIO wrapper for Akka Cluster
Scala
164
star
16

interop-cats

ZIO instances for cats-effect type classes
Scala
157
star
17

zio-direct

Direct-Style Programming for ZIO
Scala
154
star
18

zio-query

Add efficient pipelining, batching, and caching to any data source
Scala
150
star
19

zio-flow

Resilient, distributed applications powered by ZIO
Scala
142
star
20

zio-schema

Compositional, type-safe schema definitions, which enable auto-derivation of codecs and migrations.
Scala
141
star
21

izumi-reflect

TypeTag without scala-reflect. Supports Scala 2 and Scala 3.
Scala
140
star
22

zio-aws

Low level ZIO interface for the full AWS
Scala
139
star
23

zio-redis

A ZIO-based redis client
Scala
123
star
24

zio-telemetry

ZIO-powered OpenTelemetry library
Scala
112
star
25

zio-petclinic

An idiomatic pet clinic application written with ZIO.
Scala
103
star
26

zio-cache

A ZIO native cache with a simple and compositional interface
Scala
85
star
27

zio-zmx

Monitoring, Metrics and Diagnostics for ZIO
Scala
85
star
28

zio-shield

Enforce best coding practices with ZIO
Scala
82
star
29

zio-jdbc

A small, idiomatic ZIO interface to JDBC.
Scala
82
star
30

zio-sqs

ZIO-powered client for AWS SQS
Scala
82
star
31

zio-openai

Scala
66
star
32

zio-process

A simple ZIO library for interacting with external processes and command-line programs
Scala
64
star
33

zio-quickstarts

A minimal quickstart ZIO application for writing a RESTful Web Service
Scala
62
star
34

zio-analytics

Distributed stream processing using ZIO
Scala
61
star
35

zio-optics

Easily modify parts of larger data structures
Scala
57
star
36

zio-metrics-legacy

⛔️ DEPRECATED
Scala
56
star
37

interop-reactive-streams

Interoperability Layer Between ZIO and Reactive Streams
Scala
47
star
38

zio-s3

An S3 client for ZIO
Scala
44
star
39

zio-lambda

AWS Lambda Runtime built with ZIO
Scala
38
star
40

zio-dynamodb

Simple, type-safe, and efficient access to DynamoDB
Scala
37
star
41

zio-amqp

ZIO-based AMQP client for Scala
Scala
33
star
42

zio-macros

Macros to scrap ZIO boilerplate
Scala
32
star
43

zio-rocksdb

A ZIO-based interface to RocksDB.
Scala
32
star
44

zio-connect

Sources, Sinks and Pipelines for channeling data
Scala
32
star
45

zio-crypto

Fast, secure cryptographic primitives in a ZIO & ZIO Streams friendly package.
Scala
30
star
46

zio-metrics-connectors

Monitoring, Metrics and Diagnostics for ZIO
Scala
30
star
47

zio-ftp

A simple, idiomatic (S)FTP client for ZIO
Scala
29
star
48

zio-parser

Scala
28
star
49

zio-mock

Scala
27
star
50

zio-constraintless

An advanced library for building DSLs that allows defering the existence of type class instances until interpretation.
Scala
26
star
51

zio-codec

High-performance codecs for ZIO applications
Scala
23
star
52

zio-project-seed.g8

giter8 template used to start new ZIO projects for the ZIO organization
Scala
21
star
53

zio-webhooks

A microlibrary for reliable and persistent webhook delivery
Scala
20
star
54

zio-profiling

Scala
19
star
55

zio-gcp

A ZIO-based interface to Google Cloud API
Scala
18
star
56

zio-insight-ui

TypeScript
17
star
57

interop-java

Scala
17
star
58

caliban-deriving

Full-featured, robust deriving for Caliban.
Scala
16
star
59

zio-test-intellij

An optional ZIO Test runner support module for the ZIO IntelliJ plugin
Shell
16
star
60

zio-insight

Toolset for ZIO developers
Scala
15
star
61

zio-delegate

Scala
14
star
62

zio-wasm

WASM AST and syntax based on zio-parser.
Scala
14
star
63

interop-twitter

Scala
13
star
64

zio-sbt

SBT Plugins For ZIO Projects
Scala
12
star
65

zio-deriving

Scala
11
star
66

zio-memberlist

Cluster membership and failure detection
Scala
11
star
67

interop-monix

Scala
8
star
68

zio-spark

A simple, type-safe ZIO interface to Spark
Scala
8
star
69

interop-scalaz

Scala
7
star
70

interop-guava

Scala
6
star
71

zio-concurrent

Concurrency utilities for ZIO.
JavaScript
6
star
72

zio-morphir

Scala
5
star
73

zio-uring

Scala
5
star
74

interop-future

Scala
5
star
75

zio-quickstart-graphql-webservice

Quickstart for Writing GraphQL Servers
Scala
4
star
76

zio-bson

BSON library with tight ZIO integration
Scala
4
star
77

zio-cron

ZIO Cron
3
star
78

zio-simple-seed.g8

Scala
3
star
79

zio-meta

Scala
2
star
80

zio-direct-intellij

Scala
2
star
81

zio-insight-server

Scala
2
star
82

zio-quickstart-hello-world

A minimal working example of ZIO Application
Scala
2
star
83

zio-distributed

The future home of ZIO Distributed!
Scala
1
star
84

zio-docs

Experimenting with a new way of maintaining dev.zio
JavaScript
1
star