|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// Package base defines shared basic pieces of the llgo command, |
| 18 | +// in particular logging and the Command structure. |
| 19 | +package base |
| 20 | + |
| 21 | +import ( |
| 22 | + "flag" |
| 23 | + "fmt" |
| 24 | + "io" |
| 25 | + "strings" |
| 26 | +) |
| 27 | + |
| 28 | +// A Command is an implementation of a xgo command |
| 29 | +// like xgo export or xgo install. |
| 30 | +type Command struct { |
| 31 | + // Run runs the command. |
| 32 | + // The args are the arguments after the command name. |
| 33 | + Run func(cmd *Command, args []string) |
| 34 | + |
| 35 | + // UsageLine is the one-line usage message. |
| 36 | + // The words between "gop" and the first flag or argument in the line are taken to be the command name. |
| 37 | + UsageLine string |
| 38 | + |
| 39 | + // Short is the short description shown in the 'gop help' output. |
| 40 | + Short string |
| 41 | + |
| 42 | + // Flag is a set of flags specific to this command. |
| 43 | + Flag flag.FlagSet |
| 44 | + |
| 45 | + // Commands lists the available commands and help topics. |
| 46 | + // The order here is the order in which they are printed by 'gop help'. |
| 47 | + // Note that subcommands are in general best avoided. |
| 48 | + Commands []*Command |
| 49 | +} |
| 50 | + |
| 51 | +// Llcppg command |
| 52 | +var Llcppg = &Command{ |
| 53 | + UsageLine: "llcppg", |
| 54 | + Short: `llcppg aims to be a tool for automatically generating LLGo bindings for C/C++ libraries, enhancing the experience of integrating LLGo with C!`, |
| 55 | + // Commands initialized in package main |
| 56 | +} |
| 57 | + |
| 58 | +// LongName returns the command's long name: all the words in the usage line between "gop" and a flag or argument, |
| 59 | +func (c *Command) LongName() string { |
| 60 | + name := c.UsageLine |
| 61 | + if i := strings.Index(name, " ["); i >= 0 { |
| 62 | + name = name[:i] |
| 63 | + } |
| 64 | + if name == "llcppg" { |
| 65 | + return "" |
| 66 | + } |
| 67 | + return strings.TrimPrefix(name, "llcppg ") |
| 68 | +} |
| 69 | + |
| 70 | +// Name returns the command's short name: the last word in the usage line before a flag or argument. |
| 71 | +func (c *Command) Name() string { |
| 72 | + name := c.LongName() |
| 73 | + if i := strings.LastIndex(name, " "); i >= 0 { |
| 74 | + name = name[i+1:] |
| 75 | + } |
| 76 | + return name |
| 77 | +} |
| 78 | + |
| 79 | +// Usage show the command usage. |
| 80 | +func (c *Command) Usage(w io.Writer) { |
| 81 | + fmt.Fprintf(w, "%s\n\nUsage: %s\n", c.Short, c.UsageLine) |
| 82 | + |
| 83 | + // restore output of flag |
| 84 | + defer c.Flag.SetOutput(c.Flag.Output()) |
| 85 | + |
| 86 | + c.Flag.SetOutput(w) |
| 87 | + c.Flag.PrintDefaults() |
| 88 | + fmt.Fprintln(w) |
| 89 | +} |
| 90 | + |
| 91 | +// Runnable reports whether the command can be run; otherwise |
| 92 | +// it is a documentation pseudo-command. |
| 93 | +func (c *Command) Runnable() bool { |
| 94 | + return c.Run != nil |
| 95 | +} |
| 96 | + |
| 97 | +// Usage is the usage-reporting function, filled in by package main |
| 98 | +// but here for reference by other packages. |
| 99 | +// |
| 100 | +// flag.Usage func() |
| 101 | + |
| 102 | +// CmdName - "build", "install", "list", "mod tidy", etc. |
| 103 | +var CmdName string |
| 104 | + |
| 105 | +// Main runs a command. |
| 106 | +func Main(c *Command, app string, args []string) { |
| 107 | + name := c.UsageLine |
| 108 | + if i := strings.Index(name, " ["); i >= 0 { |
| 109 | + c.UsageLine = app + name[i:] |
| 110 | + } |
| 111 | + c.Run(c, args) |
| 112 | +} |
0 commit comments