In the present instance it's for a Teensy.
OK, then Arduino CLI is not currently an option unfortunately:
https://github.com/arduino/arduino-cli/issues/700but work is in progress to add support for Teensy to Arduino CLI.
But the Arduino IDE/Teensyduino command line works fine too.
So, the command would be like this?
# arduino --pref compiler.cpp.extra_flags=-Dsomething myfile.ino
You need to add the --verify flag. Other than that, yes it's fine.
Is there any documentation on the available preference names?
There is some here:
https://arduino.github.io/arduino-cli/latest/platform-specification/You can also set the properties you will find in the Arduino IDE's preferences.txt file (the path is shown at the bottom of the Arduino IDE's
File > Preferences dialog).
A few are described in the CLI documentation:
https://github.com/arduino/Arduino/blob/master/build/shared/manpage.adoc#preferencesBut you won't find mention of compiler.cpp.extra_flags in any of those. The reason is that, in addition to the properties that are used internally, the Arduino build system allows you to define arbitrary properties which have no special treatment by the Arduino IDE, but can be used in the user's build and upload recipes. compiler.cpp.extra_flags is one of these arbitrary properties.
By convention, Arduino boards platforms provide a set of these "extra flags" properties to allow the user to customize the build process by injecting arbitrary compiler flags, but there is nothing special about them and their presence is not enforced by the Arduino IDE. You can see an example of this in the Arduino AVR Boards platform here:
https://github.com/arduino/ArduinoCore-avr/blob/1.8.3/platform.txt#L42-L49Unfortunately, the Teensy boards platform is one of the rare exceptions. The closest equivalent property seems to be `build.flags.defs`. However, this is already in use by the Teensy boards platform, so if you just define it as `-Dsomething` then you will be overwriting the definition of the property as used by the Teensy board platform. Here you can see the definition of it for the Teensy 4.1:
teensy41.build.flags.defs=-D__IMXRT1062__ -DTEENSYDUINO=153
(the `teensy41` part of the property is just associating that specific definition of the property with the Teensy 4.1 board selection. The property is named build.flags.defs when it's used in the compilation recipe)
So you would likely want to append your custom -D flag to the existing flags in your command so the existing flags are preserved:
arduino --pref "build.flags.defs=-D__IMXRT1062__ -DTEENSYDUINO=153 -Dsomething" myfile.ino
I have made a proposal to give these properties more of an official, documented status here:
https://github.com/arduino/arduino-cli/issues/846And, is there a way to save it to a hex file instead of uploading it?
Yes. The `build.path` preference defines where the compiled binary will be saved. So this command:
arduino --pref "build.flags.defs=-D__IMXRT1062__ -DTEENSYDUINO=153 -Dsomething" --pref build.path=/tmp/build-path myfile.ino
Will result in the .hex file being located at tmp/build-path/myfile.ino.hex after the command finishes.