Skip to content

Commit 52788e5

Browse files
authored
main: rework usage (#4467)
main: rework usage * remove unused switch case statement and improve/add command specific usage. * limit help to 80 col width * remove inconsistent ':' * remove trailing spaces * rmove spaces woth tabs * reworkd build command Signed-off-by: leongross <leon.gross@9elements.com>
1 parent bcd4c6b commit 52788e5

File tree

1 file changed

+155
-22
lines changed

1 file changed

+155
-22
lines changed

main.go

Lines changed: 155 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,36 +1230,169 @@ func getBMPPorts() (gdbPort, uartPort string, err error) {
12301230
}
12311231
}
12321232

1233-
func usage(command string) {
1234-
switch command {
1235-
default:
1236-
fmt.Fprintln(os.Stderr, "TinyGo is a Go compiler for small places.")
1237-
fmt.Fprintln(os.Stderr, "version:", goenv.Version())
1238-
fmt.Fprintf(os.Stderr, "usage: %s <command> [arguments]\n", os.Args[0])
1239-
fmt.Fprintln(os.Stderr, "\ncommands:")
1240-
fmt.Fprintln(os.Stderr, " build: compile packages and dependencies")
1241-
fmt.Fprintln(os.Stderr, " run: compile and run immediately")
1242-
fmt.Fprintln(os.Stderr, " test: test packages")
1243-
fmt.Fprintln(os.Stderr, " flash: compile and flash to the device")
1244-
fmt.Fprintln(os.Stderr, " gdb: run/flash and immediately enter GDB")
1245-
fmt.Fprintln(os.Stderr, " lldb: run/flash and immediately enter LLDB")
1246-
fmt.Fprintln(os.Stderr, " monitor: open communication port")
1247-
fmt.Fprintln(os.Stderr, " ports: list available serial ports")
1248-
fmt.Fprintln(os.Stderr, " env: list environment variables used during build")
1249-
fmt.Fprintln(os.Stderr, " list: run go list using the TinyGo root")
1250-
fmt.Fprintln(os.Stderr, " clean: empty cache directory ("+goenv.Get("GOCACHE")+")")
1251-
fmt.Fprintln(os.Stderr, " targets: list targets")
1252-
fmt.Fprintln(os.Stderr, " info: show info for specified target")
1253-
fmt.Fprintln(os.Stderr, " version: show version")
1254-
fmt.Fprintln(os.Stderr, " help: print this help text")
1233+
const (
1234+
usageBuild = `Build compiles the packages named by the import paths, along with their
1235+
dependencies, but it does not install the results. The output binary is
1236+
specified using the -o parameter. The generated file type depends on the
1237+
extension:
1238+
1239+
.o:
1240+
Create a relocatable object file. You can use this option if you
1241+
don't want to use the TinyGo build system or want to do other custom
1242+
things.
1243+
1244+
.ll:
1245+
Create textual LLVM IR, after optimization. This is mainly useful
1246+
for debugging.
1247+
1248+
.bc:
1249+
Create LLVM bitcode, after optimization. This may be useful for
1250+
debugging or for linking into other programs using LTO.
1251+
1252+
.hex:
1253+
Create an Intel HEX file to flash it to a microcontroller.
1254+
1255+
.bin:
1256+
Similar, but create a binary file.
1257+
1258+
.wasm:
1259+
Compile and link a WebAssembly file.
1260+
1261+
(all other) Compile and link the program into a regular executable. For
1262+
microcontrollers, it is common to use the .elf file extension to indicate a
1263+
linked ELF file is generated. For Linux, it is common to build binaries with no
1264+
extension at all.`
1265+
1266+
usageRun = `Run the program, either directly on the host or in an emulated environment
1267+
(depending on -target).`
1268+
1269+
usageFlash = `Flash the program to a microcontroller. Some common flags are described below.
1270+
1271+
-target={name}:
1272+
Specifies the type of microcontroller that is used. The name of the
1273+
microcontroller is given on the individual pages for each board type
1274+
listed under Microcontrollers
1275+
(https://tinygo.org/docs/reference/microcontrollers/).
1276+
Examples: "arduino-nano", "d1mini", "xiao".
1277+
1278+
-monitor:
1279+
Start the serial monitor (see below) immediately after
1280+
flashing. However, some microcontrollers need a split second
1281+
or two to configure the serial port after flashing, and
1282+
using the "-monitor" flag can fail because the serial
1283+
monitor starts too quickly. In that case, use the "tinygo
1284+
monitor" command explicitly.`
1285+
1286+
usageMonitor = `Start the serial monitor on the serial port that is connected to the
1287+
microcontroller. If there is only a single board attached to the host computer,
1288+
the default values for various options should be sufficient. In other
1289+
situations, particularly if you have multiple microcontrollers attached, some
1290+
parameters may need to be overridden using the following flags:
1291+
1292+
-port={port}:
1293+
If there are multiple microcontroller attached, an error
1294+
message will display a list of potential serial ports. The
1295+
appropriate port can be specified by this flag. On Linux,
1296+
the port will be something like /dev/ttyUSB0 or /dev/ttyACM1.
1297+
On MacOS, the port will look like /dev/cu.usbserial-1420. On
1298+
Windows, the port will be something like COM1 or COM31.
1299+
1300+
-baudrate={rate}:
1301+
The default baud rate is 115200. Boards using the AVR
1302+
processor (e.g. Arduino Nano, Arduino Mega 2560) use 9600
1303+
instead.
1304+
1305+
-target={name}:
1306+
If you have more than one microcontrollers attached, you can
1307+
sometimes just specify the target name and let tinygo
1308+
monitor figure out the port. Sometimes, this does not work
1309+
and you have to explicitly use the -port flag.
1310+
1311+
The serial monitor intercepts several control characters for its own use instead of sending them
1312+
to the microcontroller:
1313+
1314+
Control-C: terminates the tinygo monitor
1315+
Control-Z: suspends the tinygo monitor and drops back into shell
1316+
Control-\: terminates the tinygo monitor with a stack trace
1317+
Control-S: flow control, suspends output to the console
1318+
Control-Q: flow control, resumes output to the console
1319+
Control-@: thrown away by tinygo monitor
1320+
1321+
Note: If you are using os.Stdin on the microcontroller, you may find that a CR
1322+
character on the host computer (also known as Enter, ^M, or \r) is transmitted
1323+
to the microcontroller without conversion, so os.Stdin returns a \r character
1324+
instead of the expected \n (also known as ^J, NL, or LF) to indicate
1325+
end-of-line. You may be able to get around this problem by hitting Control-J in
1326+
tinygo monitor to transmit the \n end-of-line character.`
1327+
1328+
usageGdb = `Build the program, optionally flash it to a microcontroller if it is a remote
1329+
target, and drop into a GDB shell. From there you can set breakpoints, start the
1330+
program with "run" or "continue" ("run" for a local program, continue for
1331+
on-chip debugging), single-step, show a backtrace, break and resume the program
1332+
with Ctrl-C/"continue", etc. You may need to install extra tools (like openocd
1333+
and arm-none-eabi-gdb) to be able to do this. Also, you may need a dedicated
1334+
debugger to be able to debug certain boards if no debugger is integrated. Some
1335+
boards (like the BBC micro:bit and most professional evaluation boards) have an
1336+
integrated debugger.`
1337+
1338+
usageClean = `Clean the cache directory, normally stored in $HOME/.cache/tinygo. This is not
1339+
normally needed.`
1340+
1341+
usageHelp = `Print a short summary of the available commands, plus a list of command flags.`
1342+
usageVersion = `Print the version of the command and the version of the used $GOROOT.`
1343+
usageEnv = `Print a list of environment variables that affect TinyGo (as a shell script).
1344+
If one or more variable names are given as arguments, env prints the value of
1345+
each on a new line.`
1346+
1347+
usageDefault = `TinyGo is a Go compiler for small places.
1348+
version: %s
1349+
usage: %s <command> [arguments]
1350+
commands:
1351+
build: compile packages and dependencies
1352+
run: compile and run immediately
1353+
test: test packages
1354+
flash: compile and flash to the device
1355+
gdb: run/flash and immediately enter GDB
1356+
lldb: run/flash and immediately enter LLDB
1357+
monitor: open communication port
1358+
ports: list available serial ports
1359+
env: list environment variables used during build
1360+
list: run go list using the TinyGo root
1361+
clean: empty cache directory (%s)
1362+
targets: list targets
1363+
info: show info for specified target
1364+
version: show version
1365+
help: print this help text`
1366+
)
1367+
1368+
var (
1369+
commandHelp = map[string]string{
1370+
"build": usageBuild,
1371+
"run": usageRun,
1372+
"flash": usageFlash,
1373+
"monitor": usageMonitor,
1374+
"gdb": usageGdb,
1375+
"clean": usageClean,
1376+
"help": usageHelp,
1377+
"version": usageVersion,
1378+
"env": usageEnv,
1379+
}
1380+
)
12551381

1382+
func usage(command string) {
1383+
val, ok := commandHelp[command]
1384+
if !ok {
1385+
fmt.Fprintf(os.Stderr, usageDefault, goenv.Version(), os.Args[0], goenv.Get("GOCACHE"))
12561386
if flag.Parsed() {
12571387
fmt.Fprintln(os.Stderr, "\nflags:")
12581388
flag.PrintDefaults()
12591389
}
12601390

12611391
fmt.Fprintln(os.Stderr, "\nfor more details, see https://tinygo.org/docs/reference/usage/")
1392+
} else {
1393+
fmt.Fprintln(os.Stderr, val)
12621394
}
1395+
12631396
}
12641397

12651398
func handleCompilerError(err error) {

0 commit comments

Comments
 (0)