stringargv

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2017 License: MIT Imports: 2 Imported by: 0

README

Build Status Go Report Card codecov

Introduction

The parse-string-argv package can parse string cmd to argv list like os.Argv, and you can use flag or other flag parse library like pflag.

Installation and usage

go get github.com/cloverstd/parse-string-argv

Example

package main

import (
	"flag"
	"fmt"
	"log"

	"github.com/cloverstd/parse-string-argv"
)

func Example() {
	cmd := `json -o output.json -i "input1.json input2.json" -rule-list rule.json -g`

	argv, err := stringargv.Parse(cmd)
	if err != nil {
		log.Fatalf("cannot parse cmd: %s", cmd)
	}

	output := flag.String("o", "default-output.json", "")
	input := flag.String("i", "default-input.json", "")
	ruleList := flag.String("rule-list", "default-rule-list.json", "")
	global := flag.Bool("g", false, "")

	// Be sure to remember to parse argv if you need
	// use flag.CommandLine.Parse
	// flag.Parse will parse the os.Argv
	flag.CommandLine.Parse(argv[1:])

	fmt.Println(*output)
	fmt.Println(*input)
	fmt.Println(*ruleList)
	fmt.Println(*global)
}

This example will generate the following output:

output.json
input1.json input2.json
rule.json
true

Documentation

Overview

Package stringargv implements parse string cmd to argv list

Example
cmd := `json -o output.json -i "input1.json input2.json" -rule-list rule.json -g`

argv, err := stringargv.Parse(cmd)
if err != nil {
	log.Fatalf("cannot parse cmd: %s", cmd)
}

output := flag.String("o", "default-output.json", "")
input := flag.String("i", "default-input.json", "")
ruleList := flag.String("rule-list", "default-rule-list.json", "")
global := flag.Bool("g", false, "")

// Be sure to remember to parse argv if you need
// use flag.CommandLine.Parse
// flag.Parse will parse the os.Argv
flag.CommandLine.Parse(argv[1:])

fmt.Println(*output)
fmt.Println(*input)
fmt.Println(*ruleList)
fmt.Println(*global)
Output:

output.json
input1.json input2.json
rule.json
true

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrUnClosedQuote = errors.New("invalid cmd string: unclosed quote")

ErrUnClosedQuote represents a invalid cmd string with unclosed quote

Functions

func Parse

func Parse(cmd string) (argv []string, err error)

Parse cmd with space split return the []string

func ParseSplit

func ParseSplit(cmd string, split byte) (argv []string, err error)

ParseSplit will parse cmd with custom split

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL