From 632d0c61476f0f11eab26c5cc3191c61b2053204 Mon Sep 17 00:00:00 2001 From: Nicolas Wang Date: Tue, 23 Mar 2021 22:08:46 +0800 Subject: [PATCH] split main function; --- .gitignore | 1 + Makefile | 6 +++++- cmd.c | 5 +++++ graftcp-local/main.go | 16 +++++++++++++--- main.c | 2 +- 5 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 cmd.c diff --git a/.gitignore b/.gitignore index 0841923..df3d9d7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ graftcp-local/.gopath *.o *.d +*.a diff --git a/Makefile b/Makefile index 3b18f97..f8c3eeb 100644 --- a/Makefile +++ b/Makefile @@ -47,9 +47,13 @@ TARGET = graftcp $(GRAFTCP_LOCAL_BIN) all:: $(TARGET) -graftcp: main.o util.o string-set.o + +graftcp: cmd.o libgraftcp.a $(CC) $^ -o $@ +libgraftcp.a: main.o util.o string-set.o + $(AR) rcs $@ $^ + %.o: %.c $(CC) $(CFLAGS) -c -o $@ $< diff --git a/cmd.c b/cmd.c new file mode 100644 index 0000000..88e8ecd --- /dev/null +++ b/cmd.c @@ -0,0 +1,5 @@ +int _main(int argc, char **argv); + +int main(int argc, char **argv){ + return _main(argc, argv); +} \ No newline at end of file diff --git a/graftcp-local/main.go b/graftcp-local/main.go index b87f2b8..b45e623 100644 --- a/graftcp-local/main.go +++ b/graftcp-local/main.go @@ -1,11 +1,12 @@ package main import ( - "flag" + _flag "flag" "fmt" "os" "syscall" + "context" "github.com/jedisct1/dlog" "github.com/kardianos/service" ) @@ -58,7 +59,9 @@ func (app *App) Stop(s service.Service) error { return nil } -func main() { +func local_main(cmdName string, args []string, ctx... context.Context) { + flag := _flag.NewFlagSet(cmdName, _flag.ExitOnError) + var configFile string dlog.Init("graftcp-local", dlog.SeverityInfo, "") @@ -90,7 +93,10 @@ func main() { flag.StringVar(&configFile, "config", "", "Path to the configuration file") flag.StringVar(&app.PipePath, "pipepath", "/tmp/graftcplocal.fifo", "Pipe path for graftcp to send address info") v := flag.Bool("version", false, "Print the graftcp-local version information") - flag.Parse() + if err := flag.Parse(args); err!= nil{ + fmt.Printf("Wrong command line arguments for %s: %s\n", cmdName, err.Error()) + os.Exit(1) + } if *v { fmt.Printf("graftcp-local version %s\n", version) os.Exit(0) @@ -127,3 +133,7 @@ func main() { app.Start(nil) } } + +func main(){ + local_main(os.Args[0], os.Args[1:]) +} \ No newline at end of file diff --git a/main.c b/main.c index 120653a..8541423 100644 --- a/main.c +++ b/main.c @@ -378,7 +378,7 @@ static void usage(char **argv) "\n", argv[0]); } -int main(int argc, char **argv) +int _main(int argc, char **argv) { int opt, index; bool ignore_local = true;