Skip to content

Commit

Permalink
v0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmittag committed May 25, 2023
1 parent 255491d commit b027061
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
3 changes: 3 additions & 0 deletions ReadMe.Md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ For example, the Punycode representation of a legal domain
name `1❤️.ws` is `xn--1-7iqv272g.ws`.

## What's new
### v0.2.1 (25/05/23)
* refactored cli args.

### v0.2.0 (22/05/23)
* support unicode encoding of punycode

Expand Down
68 changes: 52 additions & 16 deletions cmd/punycoder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,38 @@ import (
)

func main() {
flag.Usage = punycoderUsage
flag.Usage = printUsage
modeAscii := true
modeUnicode := flag.Bool("u", false, "convert to unicode")
if *modeUnicode == true {
modeAscii = false
}
modeVersion := flag.Bool("v", false, "print punycoder version")
modeUsage := flag.Bool("h", false, "print usage")
flag.Parse()

if *modeVersion {
printVersion()
} else if *modeUsage {
punycoderUsage()
} else if *modeUnicode {
host := parseHost(flag.Args())
fmt.Println(punycoder.EncodeUnicode(host))
} else if modeAscii {
host := parseHost(flag.Args())
fmt.Println(punycoder.EncodeAscii(host))
err := parseFlags()

if err != nil {
printUsage()
} else {
punycoderUsage()
if *modeVersion {
printVersion()
} else if *modeUsage {
printUsage()
} else if *modeUnicode {
host := parseHost(flag.Args())
fmt.Println(punycoder.EncodeUnicode(host))
} else if modeAscii {
host := parseHost(flag.Args())
fmt.Println(punycoder.EncodeAscii(host))
} else {
printUsage()
}
}
}

func punycoderUsage() {
fmt.Fprintf(os.Stdout, "Usage: punycoder host | [-u] host | [-v] | [-h]\n")
func printUsage() {
printVersion()
fmt.Fprintf(os.Stdout, "Usage: punycoder host [-v] | [-h] | [-u] host \n")
flag.PrintDefaults()
}

Expand All @@ -54,3 +59,34 @@ func parseHost(args []string) string {
}
return host
}

// ParseFlags parses the command line args, allowing flags to be
// specified after positional args.
func parseFlags() error {
return parseFlagSet(flag.CommandLine, os.Args[1:])
}

// ParseFlagSet works like flagset.Parse(), except positional arguments are not
// required to come after flag arguments.
func parseFlagSet(flagset *flag.FlagSet, args []string) error {
var positionalArgs []string
for {
if err := flagset.Parse(args); err != nil {
return err
}
// Consume all the flags that were parsed as flags.
args = args[len(args)-flagset.NArg():]
if len(args) == 0 {
break
}
// There's at least one flag remaining and it must be a positional arg since
// we consumed all args that were parsed as flags. Consume just the first
// one, and retry parsing, since subsequent args may be flags.
positionalArgs = append(positionalArgs, args[0])
args = args[1:]
}
// Parse just the positional args so that flagset.Args()/flagset.NArgs()
// return the expected value.
// Note: This should never return an error.
return flagset.Parse(positionalArgs)
}
2 changes: 1 addition & 1 deletion punycoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"golang.org/x/net/idna"
)

const Version string = "v0.2.0"
const Version string = "v0.2.1"

func EncodeAscii(p string) string {
al, _ := idna.ToASCII(p)
Expand Down

0 comments on commit b027061

Please sign in to comment.