Skip to content

Instantly share code, notes, and snippets.

@twolodzko
Created April 28, 2021 15:56
Show Gist options
  • Save twolodzko/a97782be0d0bc7b65780df231fc4ea1b to your computer and use it in GitHub Desktop.
Save twolodzko/a97782be0d0bc7b65780df231fc4ea1b to your computer and use it in GitHub Desktop.
Print command line flags
package main
import (
"fmt"
"os"
)
func main() {
if len(os.Args) > 1 && os.Args[1] == "--help" {
args := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
fmt.Printf("Usage:\n\n%s [--] [%s]...\n\n", os.Args[0], args)
out := "Args:\n -- prints space\n"
for _, r := range args {
ch := string(r)
out += fmt.Sprintf(" -%s prints %s\n", ch, ch)
}
fmt.Print(out)
return
}
var runes []rune
for _, arg := range os.Args[1:] {
if arg == "--" {
runes = append(runes, ' ')
continue
}
for _, r := range arg {
if r != '-' {
runes = append(runes, r)
}
}
}
fmt.Println(string(runes))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment