12_arg
Important
Visit https://aerosand.cc for the latest updates.
0. Preface
Many OpenFOAM applications can accept runtime parameters via the command line. Although users may sometimes find this unfamiliar, mastering command-line usage enables efficient and flexible handling of various problems.
We will start with C++ by briefly introducing command-line arguments in C++, and then further discuss command-line arguments in OpenFOAM.
This section primarily discusses:
- Understanding the OpenFOAM command line
- Understanding command-line arguments in C++
- Understanding the functionality and options of command-line arguments
- Compiling and running an arg project
1. OpenFOAM Command Line
Reference: https://doc.openfoam.com/2312/fundamentals/command-line/
The basic usage format of the OpenFOAM command line is as follows:
<application> <options> <arguments>For example, a typical command-line usage in practice is:
blockMesh -case debug_caseUsing the -help option allows viewing more command-line options. For instance, entering blockMesh -help in the terminal produces the following output:
Usage: blockMesh [OPTIONS]
Options:
-case <dir> Case directory (instead of current directory)
-dict <file> Alternative blockMeshDict
-merge-points Geometric point merging instead of topological merging
[default for 1912 and earlier].
-no-clean Do not remove polyMesh/ directory or files
-region <name> Specify mesh region (default: region0)
-sets Write cellZones as cellSets too (for processing purposes)
-time <time> Specify a time to write mesh to (default: constant)
-verbose Force verbose output. (Can be used multiple times)
-write-vtk Write topology as VTU file and exit
-doc Display documentation in browser
-help Display short help and exit
-help-compat Display compatibility options and exit
-help-full Display full help and exit
Block mesh generator.
The ordering of vertex and face labels within a block as shown below.
For the local vertex numbering in the sequence 0 to 7:
Faces 0, 1 (x-direction) are left, right.
Faces 2, 3 (y-direction) are front, back.
Faces 4, 5 (z-direction) are bottom, top.
7 ---- 6
f5 |\ :\ f3
| | 4 ---- 5 \
| 3.|....2 | \
| \| \| f2
f4 0 ---- 1
Y Z
\ | f0 ------ f1
\|
o--- X
Using: OpenFOAM-2406 (2406) - visit www.openfoam.com
Build: _9bfe8264-20241212 (patch=241212)
Arch: LSB;label=32;scalar=64Generally, the command line is also heavily used during data processing and post-processing.
For example, using:
foamPostProcess -helpSpecific usage techniques will not be elaborated here and will be discussed separately in the future.
Let us first briefly review the use of command-line arguments in C++.
2. Project Preparation
Run the following commands in the terminal to create the project:
ofsp
mkdir ofsp_12_arg
code ofsp_12_argUsing VS Code, press F1 and enter Create C++ project, enter ofsp_12_arg as the project name, select /ofsp as the parent directory, and initialize the project.
Run the following command in the terminal to test the initial project:
make runThe terminal output Hello world! indicates that the initial project runs successfully.
3. Command-Line Arguments
When we first learn C++, the parameters of the main function are typically left empty, as follows:
...
int main() // Omitted parameter list
{
...
}When delving deeper into C++ development, we learn that the more general form of the main function is:
...
int main(int argc, char *argv[]) {}
// Or
int main(int argc, char **argv) {}Where:
argcstands for “argument count,” storing the number of arguments passed to the main function when the program is executed.argvstands for “argument vector,” storing pointers to the specific arguments passed to the main function when the program is executed. Each pointer points to a specific argument.argv[0]points to the full path name of the program at runtime.argv[1]points to the first string after the program name in the command line at runtime.argv[2]points to the second string after the program name in the command line at runtime.- And so on for the rest.
4. Argument Order
The main source code in src/main.cpp is as follows:
| |
Run the following command in the terminal to compile and run:
make runThe terminal output is as follows:
Number of arguments = 1
Argument 0: ./output/mainIf additional arguments are provided at runtime, for example:
./output/main hi hey helloThe terminal output is as follows:
Number of arguments = 4
Argument 0: ./output/main
Argument 1: hi
Argument 2: hey
Argument 3: helloFrom the results, we can see that argc is, of course, the total number of arguments, and argv[0] is the application name itself, with other arguments following in order.
For example, for the following command line:
blockMesh -case debug_caseHere, argc equals 3, with argv[0] being blockMesh, argv[1] being -case, and argv[2] being debug_case.
In summary, each parameter in the command line can be accessed in the program through the main function parameters, allowing them to participate in the program’s execution and computation.
5. Argument Functionality
Based on this project, we modify the main source code to add functionality to the command-line arguments.
The main source code in src/main.cpp is as follows:
| |
Run the following command in the terminal to compile the program:
makeRun the following command to view the default output:
./output/mainThe terminal output is as follows:
Number of arguments = 1
Argument 0: ./output/main
Usage: ./output/main <filename> [mode]
Modes: read (default), writeRun the following command to write to a file:
./output/main data.txt writeThe terminal output prompt is as follows:
Number of arguments = 3
Argument 0: ./output/main
Argument 1: data.txt
Argument 2: write
Enter content to write (empty line to finish):Enter test information in the terminal, and press Enter with a blank line to finish input:
ofsp
This is a ofsp test.Run the following command to read from the file:
./output/main data.txt readThe terminal output is as follows:
Number of arguments = 3
Argument 0: ./output/main
Argument 1: data.txt
Argument 2: read
File content:
ofsp
This is a ofsp test.It can be seen that by entering different command-line arguments, different functionalities of the program can be achieved.
6. Argument Options
We continue developing this project to implement command-line argument options.
The main source code in src/main.cpp is as follows:
| |
Run the following command in the terminal to compile the program:
makeRun the following command to view the help information:
./output/main -hThe terminal output is as follows:
Usage: ./output/main [OPTIONS] <filename> [mode]
A simple file read/write utility @ Aerosand
Arguments:
filename Name of the file to read/write
mode Operation mode: read or write (default: read)
Options:
-h, --help Display this help message
-l Show line numbers when reading
-v, --version Display version information
-a, --append Use append mode when writing (default)
-o, --overwrite Use overwrite mode when writing
Examples:
./output/main data.txt read
./output/main -l notes.txt
./output/main -o log.txt write
./output/main -a data.txt writeRun the following command to view the version information:
./output/main --versionThe terminal output is as follows:
Version v1.0 @ Aerosand
This is a ofsp program from AerosandRun the following command to overwrite a file:
./output/main data.txt write -o
Enter content to write (empty line to finish):
ofsp
Content written to fileRun the following command to read the file:
./output/main -l data.txtThe terminal output is as follows:
File content:
1: ofspRun the following command to append to a file:
./output/main data.txt write -a
Enter content to write (empty line to finish):
This is a ofsp test from Aerosand.
Content appended to fileRun the following command to read the file:
./output/main data.txt read -lThe terminal output is as follows:
File content:
1: ofsp
2: This is a ofsp test from Aerosand.It can be seen that this project can execute various command-line arguments and command-line options, such as displaying line numbers in the output and choosing how to write file content.
Tip
The above code does not consider architecture or optimization; it is intended to help readers simply understand the basic concepts of command-line arguments.
7. Summary
This project discussed the basic usage and implementation of command-line arguments in C++. I believe readers now have a better understanding of command-line arguments. Based on these discussions, the next section will transition to discussing command-line arguments in OpenFOAM.
This section has completed the following discussions:
- Understanding the OpenFOAM command line
- Understanding command-line arguments in C++
- Understanding the functionality and options of command-line arguments
- Compiling and running an arg project
Support us
Tip
Hopefully, the sharing here can be helpful to you.
If you find this content helpful, your comments or donations would be greatly appreciated. Your support helps ensure the ongoing updates, corrections, refinements, and improvements to this and future series, ultimately benefiting new readers as well.
The information and message provided during donation will be displayed as an acknowledgment of your support.
Copyright @ 2026 Aerosand
- Course (text, images, etc.):CC BY-NC-SA 4.0
- Code derived from OpenFOAM:GPL v3
- Other code:MIT License
