How to use the command `astyle` (with examples)
The command astyle
is a source code indenter, formatter, and beautifier for the C, C++, C# and Java programming languages. It allows you to automatically format your code according to different styles and indentation rules.
Use case 1: Apply the default style
Code:
astyle source_file
Motivation: You can use this command to apply the default style to your source code file, with 4 spaces per indent and no additional formatting changes.
Explanation: When you run the command astyle source_file
, the astyle
executable is invoked with the source_file
as the argument. This applies the default formatting rules to the source_file
. The original file is not modified, instead, a copy of the original file is created with an “.orig” appended to the original file name.
Example output: The source code file source_file
will be indented with 4 spaces per indent and no additional formatting changes.
Use case 2: Apply the Java style with attached braces
Code:
astyle --style=java path/to/file
Motivation: If you are working on a Java project and want to follow the common Java coding conventions, you can use this command to apply the Java style to your code.
Explanation: By adding the argument --style=java
to the astyle
command, you specify that you want to apply the Java style to the code in the file located at path/to/file
. The Java style includes attached braces, where opening braces are placed on the same line as the statement and closing braces are placed on a new line.
Example output: The code in the file located at path/to/file
will be formatted according to the Java style, with attached braces.
Use case 3: Apply the allman style with broken braces
Code:
astyle --style=allman path/to/file
Motivation: If you prefer the Allman style, which uses broken braces where opening braces are placed on a new line and closing braces are placed on a new line, you can use this command.
Explanation: By adding the argument --style=allman
to the astyle
command, you specify that you want to apply the Allman style to the code in the file located at path/to/file
. The Allman style includes broken braces.
Example output: The code in the file located at path/to/file
will be formatted according to the Allman style, with broken braces.
Use case 4: Apply a custom indent using spaces
Code:
astyle --indent=spaces=number_of_spaces path/to/file
Motivation: If you want to apply a custom indent using spaces, you can use this command. You can choose the number of spaces for each indent.
Explanation: By adding the argument --indent=spaces=number_of_spaces
to the astyle
command, you specify that you want to apply a custom indent using spaces to the code in the file located at path/to/file
. Replace number_of_spaces
with the desired number of spaces for each indent, ranging from 2 to 20.
Example output: The code in the file located at path/to/file
will be formatted with the specified number of spaces for each indent.
Use case 5: Apply a custom indent using tabs
Code:
astyle --indent=tab=number_of_tabs path/to/file
Motivation: If you prefer using tabs for indentation instead of spaces, you can use this command to apply a custom indent using tabs. You can choose the number of tabs for each indent.
Explanation: By adding the argument --indent=tab=number_of_tabs
to the astyle
command, you specify that you want to apply a custom indent using tabs to the code in the file located at path/to/file
. Replace number_of_tabs
with the desired number of tabs for each indent, ranging from 2 to 20.
Example output: The code in the file located at path/to/file
will be formatted with the specified number of tabs for each indent.