Format Command
The format command formats C# code using the built-in formatter and syntax emitters.
Usage
bsharp format <INPUT> [--write <BOOL>] [--print] [--newline-mode lf|crlf] \
[--max-consecutive-blank-lines <N>] [--blank-line-between-members <BOOL>] \
[--trim-trailing-whitespace <BOOL>] [--emit-trace] [--emit-trace-file <FILE>]
Arguments
<INPUT> (required)
- Path to
.csfile or directory - When a directory is given, formats all
.csfiles recursively - Hidden directories and
bin/,obj/,target/are skipped
Options
--write, -w <BOOL>
- Write changes to files in-place
- Default:
true - When
falseand<INPUT>is a single file, the formatted content is printed to stdout - When
falseand formatting differences are found for multiple files, exits with code2
--print
- Always print formatted output for a single-file input and exit
- Useful for piping to other tools; does not write to disk regardless of
--write
--newline-mode <MODE>
- Newline mode:
lf(default) orcrlf
--max-consecutive-blank-lines <N>
- Maximum consecutive blank lines to keep (default:
1)
--blank-line-between-members <BOOL>
- Insert a blank line between type members (default:
true)
--trim-trailing-whitespace <BOOL>
- Trim trailing whitespace (default:
true)
--emit-trace
- Enable emission tracing (JSONL) for debugging formatter behavior
- Can also be enabled via environment variable
BSHARP_EMIT_TRACE=1
--emit-trace-file <FILE>
- Path to write the trace JSONL (defaults to stdout when omitted)
Examples
# Format a single file in-place
bsharp format Program.cs
# Print formatted output to stdout (do not write)
bsharp format Program.cs --write false
# Force printing formatted output even if --write is not set
bsharp format Program.cs --print
# Format a directory recursively
bsharp format src/
# Use CRLF newlines and avoid extra blank lines
bsharp format Program.cs --newline-mode crlf --max-consecutive-blank-lines 1
# Enable emission tracing to a file
bsharp format Program.cs --emit-trace --emit-trace-file format_trace.jsonl
Implementation
- Command:
src/bsharp_cli/src/commands/format.rs - Formatter:
bsharp_syntax::FormatterwithFormatOptions - Emission tracing is controlled by CLI flags or
BSHARP_EMIT_TRACEand recorded as JSONL. - Files that fail to parse are skipped; a summary is printed and they are not modified.