📄 astyle_main.cpp
字号:
(*_err) << " default indent option\n";
(*_err) << " If no indentation option is set,\n";
(*_err) << " the default option of 4 spaces will be used.\n";
(*_err) << endl;
(*_err) << " --indent=spaces=# OR -s#\n";
(*_err) << " Indent using # spaces per indent. Not specifying #\n";
(*_err) << " will result in a default of 4 spaces per indent.\n";
(*_err) << endl;
(*_err) << " --indent=tab OR --indent=tab=# OR -t OR -t#\n";
(*_err) << " Indent using tab characters, assuming that each\n";
(*_err) << " tab is # spaces long. Not specifying # will result\n";
(*_err) << " in a default assumption of 4 spaces per tab.\n";
(*_err) << endl;
(*_err) << " --force-indent=tab=# OR -T#\n";
(*_err) << " Indent using tab characters, assuming that each\n";
(*_err) << " tab is # spaces long. Force tabs to be used in areas\n";
(*_err) << " Astyle would prefer to use spaces.\n";
(*_err) << endl;
(*_err) << " default brackets option\n";
(*_err) << " If no brackets option is set,\n";
(*_err) << " the brackets will not be changed.\n";
(*_err) << endl;
(*_err) << " --brackets=break OR -b\n";
(*_err) << " Break brackets from pre-block code (i.e. ANSI C/C++ style).\n";
(*_err) << endl;
(*_err) << " --brackets=attach OR -a\n";
(*_err) << " Attach brackets to pre-block code (i.e. Java/K&R style).\n";
(*_err) << endl;
(*_err) << " --brackets=linux OR -l\n";
(*_err) << " Break definition-block brackets and attach command-block\n";
(*_err) << " brackets.\n";
(*_err) << endl;
(*_err) << " --brackets=break-closing OR -y\n";
(*_err) << " Break brackets before closing headers (e.g. 'else', 'catch', ...).\n";
(*_err) << " Should be appended to --brackets=attach or --brackets=linux.\n";
(*_err) << endl;
(*_err) << "Indentation options:\n";
(*_err) << "--------------------\n";
(*_err) << " --indent-classes OR -C\n";
(*_err) << " Indent 'class' blocks, so that the inner 'public:',\n";
(*_err) << " 'protected:' and 'private: headers are indented in\n";
(*_err) << " relation to the class block.\n";
(*_err) << endl;
(*_err) << " --indent-switches OR -S\n";
(*_err) << " Indent 'switch' blocks, so that the inner 'case XXX:'\n";
(*_err) << " headers are indented in relation to the switch block.\n";
(*_err) << endl;
(*_err) << " --indent-cases OR -K\n";
(*_err) << " Indent case blocks from the 'case XXX:' headers.\n";
(*_err) << " Case statements not enclosed in blocks are NOT indented.\n";
(*_err) << endl;
(*_err) << " --indent-brackets OR -B\n";
(*_err) << " Add extra indentation to '{' and '}' block brackets.\n";
(*_err) << endl;
(*_err) << " --indent-blocks OR -G\n";
(*_err) << " Add extra indentation entire blocks (including brackets).\n";
(*_err) << endl;
(*_err) << " --indent-namespaces OR -N\n";
(*_err) << " Indent the contents of namespace blocks.\n";
(*_err) << endl;
(*_err) << " --indent-labels OR -L\n";
(*_err) << " Indent labels so that they appear one indent less than\n";
(*_err) << " the current indentation level, rather than being\n";
(*_err) << " flushed completely to the left (which is the default).\n";
(*_err) << endl;
(*_err) << " --indent-preprocessor OR -w\n";
(*_err) << " Indent multi-line #define statements.\n";
(*_err) << endl;
(*_err) << " --max-instatement-indent=# OR -M#\n";
(*_err) << " Indent a maximal # spaces in a continuous statement,\n";
(*_err) << " relative to the previous line.\n";
(*_err) << endl;
(*_err) << " --min-conditional-indent=# OR -m#\n";
(*_err) << " Indent a minimal # spaces in a continuous conditional\n";
(*_err) << " belonging to a conditional header.\n";
(*_err) << endl;
(*_err) << "Formatting options:\n";
(*_err) << "-------------------\n";
(*_err) << " --break-blocks OR -f\n";
(*_err) << " Insert empty lines around unrelated blocks, labels, classes, ...\n";
(*_err) << endl;
(*_err) << " --break-blocks=all OR -F\n";
(*_err) << " Like --break-blocks, except also insert empty lines \n";
(*_err) << " around closing headers (e.g. 'else', 'catch', ...).\n";
(*_err) << endl;
(*_err) << " --break-elseifs OR -e\n";
(*_err) << " Break 'else if()' statements into two different lines.\n";
(*_err) << endl;
(*_err) << " --pad=oper OR -p\n";
(*_err) << " Insert space paddings around operators.\n";
(*_err) << endl;
(*_err) << " --pad=paren OR -P\n";
(*_err) << " Insert space padding around parenthesis on both the outside\n";
(*_err) << " and the inside.\n";
(*_err) << endl;
(*_err) << " --pad=paren-out OR -d\n";
(*_err) << " Insert space padding around parenthesis on the outside only.\n";
(*_err) << endl;
(*_err) << " --pad=paren-in OR -D\n";
(*_err) << " Insert space padding around parenthesis on the inside only.\n";
(*_err) << endl;
(*_err) << " --unpad=paren OR -U\n";
(*_err) << " Remove unnecessary space padding around parenthesis. This\n";
(*_err) << " can be used in combination with the 'pad' options above.\n";
(*_err) << endl;
(*_err) << " --one-line=keep-statements OR -o\n";
(*_err) << " Don't break lines containing multiple statements into\n";
(*_err) << " multiple single-statement lines.\n";
(*_err) << endl;
(*_err) << " --one-line=keep-blocks OR -O\n";
(*_err) << " Don't break blocks residing completely on one line.\n";
(*_err) << endl;
(*_err) << " --convert-tabs OR -V\n";
(*_err) << " Convert tabs to spaces.\n";
(*_err) << endl;
(*_err) << " --fill-empty-lines OR -E\n";
(*_err) << " Fill empty lines with the white space of their\n";
(*_err) << " previous lines.\n";
(*_err) << endl;
(*_err) << " --mode=c OR -c\n";
(*_err) << " Indent a C, C++ or C# source file (this is the default).\n";
(*_err) << endl;
(*_err) << " --mode=java OR -j\n";
(*_err) << " Indent a Java(TM) source file.\n";
(*_err) << endl;
(*_err) << "Other options:\n";
(*_err) << "--------------\n";
(*_err) << " --suffix=####\n";
(*_err) << " Append the suffix #### instead of '.orig' to original filename.\n";
(*_err) << endl;
(*_err) << " --suffix=none OR -n\n";
(*_err) << " Do not retain a backup of the original file.\n";
(*_err) << endl;
(*_err) << " --options=####\n";
(*_err) << " Specify an options file #### to read and use.\n";
(*_err) << endl;
(*_err) << " --options=none\n";
(*_err) << " Disable the default options file.\n";
(*_err) << " Only the command-line parameters will be used.\n";
(*_err) << endl;
(*_err) << " --preserve-date OR -Z\n";
(*_err) << " The date and time modified will not be changed in the formatted file.\n";
(*_err) << endl;
(*_err) << " --errors-to-stdout OR -X\n";
(*_err) << " Print errors and help information to standard-output rather than\n";
(*_err) << " to standard-error.\n";
(*_err) << endl;
(*_err) << " --version OR -v\n";
(*_err) << " Print version number.\n";
(*_err) << endl;
(*_err) << " --help OR -h OR -?\n";
(*_err) << " Print this help message.\n";
(*_err) << endl;
(*_err) << "Default options file:\n";
(*_err) << "---------------------\n";
(*_err) << " Artistic Style looks for a default options file in the\n";
(*_err) << " following order:\n";
(*_err) << " 1. The contents of the ARTISTIC_STYLE_OPTIONS environment\n";
(*_err) << " variable if it exists.\n";
(*_err) << " 2. The file called .astylerc in the directory pointed to by the\n";
(*_err) << " HOME environment variable ( i.e. $HOME/.astylerc ).\n";
(*_err) << " 3. The file called astylerc in the directory pointed to by the\n";
(*_err) << " USERPROFILE environment variable ( i.e. %USERPROFILE%\\astylerc ).\n";
(*_err) << " If a default options file is found, the options in this file\n";
(*_err) << " will be parsed BEFORE the command-line options.\n";
(*_err) << " Long options within the default option file may be written without\n";
(*_err) << " the preliminary '--'.\n";
(*_err) << endl;
}
int main(int argc, char *argv[])
{
ASFormatter formatter;
vector<string> fileNameVector;
vector<string> optionsVector;
string optionsFileName = "";
string arg;
bool ok = true;
bool shouldPrintHelp = false;
bool shouldParseOptionsFile = true;
_modeManuallySet = false;
_msg << "\nArtistic Style " << _version << endl;
// manage flags
for (int i = 1; i < argc; i++)
{
arg = string(argv[i]);
if ( IS_OPTION(arg, "--options=none") )
{
shouldParseOptionsFile = false;
}
else if ( isParamOption(arg, "--options=") )
{
optionsFileName = GET_PARAM(arg, "--options=");
}
else if ( IS_OPTION(arg, "-h")
|| IS_OPTION(arg, "--help")
|| IS_OPTION(arg, "-?") )
{
shouldPrintHelp = true;
}
else if (arg[0] == '-')
{
optionsVector.push_back(arg);
}
else // file-name
{
fileNameVector.push_back(arg);
}
}
// parse options file
if (shouldParseOptionsFile)
{
if (optionsFileName.compare("") == 0)
{
char* env = getenv("ARTISTIC_STYLE_OPTIONS");
if (env != NULL)
optionsFileName = string(env);
}
if (optionsFileName.compare("") == 0)
{
char* env = getenv("HOME");
if (env != NULL)
optionsFileName = string(env) + string("/.astylerc");
}
if (optionsFileName.compare("") == 0)
{
char* env = getenv("USERPROFILE");
if (env != NULL)
optionsFileName = string(env) + string("/astylerc");
}
if (optionsFileName.compare("") != 0)
{
ifstream optionsIn(optionsFileName.c_str());
if (optionsIn)
{
_msg << "Using default options file " << optionsFileName << endl;
vector<string> fileOptionsVector;
importOptions(optionsIn, fileOptionsVector);
ok = parseOptions(formatter,
fileOptionsVector.begin(),
fileOptionsVector.end(),
string("Unknown option in default options file: "));
}
optionsIn.close();
if (!ok)
{
(*_err) << "For help on options, type 'astyle -h' " << endl;
}
}
}
// parse options from command line
ok = parseOptions(formatter,
optionsVector.begin(),
optionsVector.end(),
string("Unknown command line option: "));
if (!ok)
{
(*_err) << "For help on options, type 'astyle -h' \n" << endl;
exit(1);
}
if (shouldPrintHelp)
{
printHelp();
exit(0);
}
// if no files have been given, use cin for input and cout for output
// this is used to format text for text editors like TextWrangler
// do NOT display any console messages when this branch is used
if (fileNameVector.empty())
{
ASStreamIterator<istream> streamIterator(&cin);
formatter.init(&streamIterator);
while (formatter.hasMoreLines())
{
cout << formatter.nextLine();
if (formatter.hasMoreLines())
cout << streamIterator.outputEOL;
}
cout.flush();
}
else
{
// indent the given files
cout << _msg.str().c_str();
clock_t startTime = clock();
for (size_t i = 0; i < fileNameVector.size(); i++)
{
string originalFileName = fileNameVector[i];
string inFileName = originalFileName + _suffix;
remove(inFileName.c_str()); // remove the old .orig if present
// check if the file is present before rename
ifstream inCheck(originalFileName.c_str());
if (!inCheck)
error("Could not open input file", originalFileName.c_str());
inCheck.close();
if (rename(originalFileName.c_str(), inFileName.c_str()) < 0)
error("Could not rename ", string(originalFileName + " to " + inFileName).c_str());
ifstream in(inFileName.c_str(), ios::binary);
if (!in)
error("Could not open input file", inFileName.c_str());
ofstream out(originalFileName.c_str(), ios::binary);
if (!out)
error("Could not open output file", originalFileName.c_str());
// Unless a specific language mode has been, set the language mode
// according to the file's suffix.
if (!_modeManuallySet)
{
if (stringEndsWith(originalFileName, string(".java")))
formatter.setJavaStyle();
else if (stringEndsWith(originalFileName, string(".cs")))
formatter.setSharpStyle();
else
formatter.setCStyle();
}
// display file formatting message and save the filename
cout << "formatting " << originalFileName.c_str() << endl;
size_t fname = originalFileName.find_last_of("/\\");
if (fname == string::npos)
fname = 0;
else
fname +=1;
formatter.fileName = originalFileName.substr(fname);
ASStreamIterator<istream> streamIterator(&in);
formatter.init(&streamIterator);
while (formatter.hasMoreLines())
{
out << formatter.nextLine();
// the last line does not get an eol
if (formatter.hasMoreLines())
out << streamIterator.outputEOL;
}
out.flush();
out.close();
in.close();
// change date modified to original file date
if (_preserveDate)
preserveFileDate(inFileName.c_str(), originalFileName.c_str());
if (_purgeOrigIn)
remove(inFileName.c_str());
}
// all files formatted
clock_t stopTime = clock();
float secs = (float) (stopTime - startTime) / CLOCKS_PER_SEC;
// show tenths of a second if time is less than 20 seconds
cout.precision(2);
if (secs >= 100 || (secs >= 10 && secs < 20))
cout.precision(3);
cout << "total time " << secs << " seconds" << endl;
cout.precision(0);
cout << endl;
}
return 0;
}
#endif
// ************************* end of console functions *****************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -