Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created May 13, 2020 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/d9860397e7538086a24d08de18f73e00 to your computer and use it in GitHub Desktop.
Save podhmo/d9860397e7538086a24d08de18f73e00 to your computer and use it in GitHub Desktop.
from egoist.app import App, SettingsDict
settings: SettingsDict = {"rootdir": "cmd/", "here": __file__}
app = App(settings)
app.include("egoist.directives.define_cli")
@app.define_cli("egoist.generators.clikit:walk")
def hello(*, name: str, age: int = 20, who: str = "foo") -> None:
"""hello message"""
from egoist.generators.clikit import runtime, clikit
options = runtime.get_cli_options()
options.name.help = "the name of target person"
options.age.help = "age of subject"
options.who.help = "name of subject"
with runtime.generate(clikit) as m:
hello_pkg = m.import_("m/internal/hello")
m.stmt(hello_pkg.Hello(name, age, who)) # m.stmtを忘れずに
if __name__ == "__main__":
app.run()
module m
go 1.14
package hello
import "fmt"
// Hello ...
func Hello(name string, age int, who string) {
fmt.Printf("%s(%d): hello %s\n", who, age, name)
}
package main
import (
"flag"
"fmt"
"os"
"log"
"m/internal/hello"
)
// this file is generated by egoist.generators.clikit
// Option ...
type Option struct {
Name string // for `-name`
Age int // for `-age`
Who string // for `-who`
Args []string // cmd.Args
}
func main() {
opt := &Option{}
cmd := flag.NewFlagSet("hello", flag.ContinueOnError)
cmd.Usage = func() {
fmt.Fprintln(cmd.Output(), `hello - hello message`)
fmt.Fprintln(cmd.Output(), "")
fmt.Fprintln(cmd.Output(), "Usage:")
cmd.PrintDefaults()
}
cmd.StringVar(&opt.Name, "name", "", "the name of target person")
cmd.IntVar(&opt.Age, "age", 20, "age of subject")
cmd.StringVar(&opt.Who, "who", "foo", "name of subject")
if err := cmd.Parse(os.Args[1:]); err != nil {
if err != flag.ErrHelp {
cmd.Usage()
}
os.Exit(1)
}
opt.Args = cmd.Args()
if err := run(opt); err != nil {
log.Fatalf("!!%+v", err)
}
}
func run(opt *Option) error {
hello.Hello(opt.Name, opt.Age, opt.Who)
return nil
}
gen:
python definitions.py generate
.
├── Makefile
├── cmd
│   └── hello
│   └── main.go
├── definitions.py
├── go.mod
├── internal
│   └── hello
│   └── hello.go
└── structure.txt
4 directories, 6 files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment