- Use
MSyntaxobject to add and define all flag and arguments you need. - Register a new syntax object.
- Use
MArgDatabaseorMArgParserto analisys user arguments.
- Use
MSyntax::addFlagto add new flag with 3 arguments
short name(brief name), long name and argument type. - The flag short name less than 4 character.
- The flag long name gather than 3 character.
- A flag can be multi-usage, if you call
MSyntax::makeFlagMultiUse. - A flag can be receive 6 parameter at most, They can be difference type.
- The special flag -e (-edit) and -q (-query) enable them use
enableEditandenableQuery.
Define argument have TWO ways : command argument and objects.
- Use
MSyntax::addArgto add a new command argument.
- Use
MSyntax::setObjectTypeto add a new command argument. - It can be define minimum and maximum on number of objects.
- Pass
MSyntax::kStringObjectsenum it no check object exists in the scene
and get it asMStringArray. - Pass
MSyntax::kSelectionListenum it can check object exists in the scene
and get it asMSelectionList. - Pass
MSyntax::kNoneenum it have no any parameter.
command (-edit|-query) -flag1 arg1 arg2 arg3 -flag2 arg1 arg2 arg3 argument;
| Segments | Description |
|---|---|
| command | The command name identify. |
| -edit | A special flag, could be represents edit mode. |
| -query | A special flag, could be represents query mode. |
| -flag | There are represent all flags and arguments. |
| argument | That is a command argument at the end of command. |
- The flag can be multi-use if you set it to multi-usage.
The MSyntax::addFlag can be add a new flag in command.
We need specific its type in 3rd argument by MSyntax's enum.
For example, We want to add a flag called "-f" and "-flag".
And it is a "string" type argument.
// In new syntax method.
stat = syntax.addFlag("-f", "-flag", MSyntax::kString);The MSyntax::addArg can be add a new argument in command.
We need specific its type by MSyntax's enum.
// In new syntax method.
stat = syntax.addArg(MSyntax::kString);// In new syntax method.
syntax.useSelectionAsDefault(true);And you have to retrieve them from MArgDatabase::getObjects
// In parse method.
MSelectionList selectionList;
stat = parser.getObjects(selections);MSyntax CustomCmd::newSyntax()
{
MStatus stat = MS::kSuccess;
MSyntax syntax;
stat = syntax.addFlag("-a", "-argument", MSyntax::kString);
if (stat != MS::kSuccess) {
return syntax;
}
// Use MSyntax::makeFlagMultiUse(flag)
stat = syntax.makeFlagMultiUse("-a");
if (stat != MS::kSuccess) {
return syntax;
}
return syntax
}In CustomCmd::parseSyntax(const MArgList & argList)
For example -
In below case, we wil get a integer "4", because we have 4 difference flags.
// In parse method
unsigned int num_of_flag_used = parser.numOfFlagUsed();// In MEL
command -flagA 1 -flagB 2 -flagC 3 -flagD 4;
Attention : If you want use numOfFlagUses method to parse, You must set it to multi-usage
For example -
In below case, we will get a integer "3". because this flag is 3 times appears in command.
// In parse method
unsigned int num_of_flag_uses = parser.numOfFlagUses("-flagA");// In MEL
command -flagA arg1 -flagA arg2 -flagA arg3;
if (parser.isFlagSet("-myflag"))
{
// Do next...
}Attention : If you want to use isEdit, you must use enableEdit in MSyntax
Attention : If you want to use isQuery, you must use enableQuery in MSyntax
if (parser.isEdit())
{
// Do edit...
}
if (parser.isQuery())
{
// Do query...
}Attention : getFlagArgument just available in C++
In this case, we get "abcdef".
// In c++
MStatus stat;
MString temp;
stat = parser.getFlagArgument("-flag", 0, temp);// In mel
command -flag abcdef;
Attention : getCommandArgument just available in C++
In this case, we get "aaa".
// In c++
MStatus stat;
MString temp;
stat = parser.getCommandArgument(0, temp);// In mel
command -flag abcdef "aaa";
The getFlagArgumentPosition can be get the position of specific flag.
// In cpp
unsigned int pos;
parser.getFlagArgumentPosition("-flagA", 0, pos);
// pos is get 0
parser.getFlagArgumentPosition("-flagA", 1, pos);
// pos is get 2
parser.getFlagArgumentPosition("-flagC", 0, pos);
// pos is get 6// In mel
command -flagA abc -flagA efg -flagB 123 -flagC 456;
| Position | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| Segments | -flagA | abc | -flagA | efg | -flagB | 123 | -flagC | 456 |
MStatus CustomCmd::parseSyntax(const MArgList & argList)
{
MStatus stat = MS::kSuccess;
MArgDatabase parser(syntax(), argList, &stat);
if (stat != MS::kSuccess) {
return stat;
}
if (parser.isFlagSet("-a", &stat) && stat == MS::kSuccess) {
unsigned int num_flag_used = parser.numOfFlagUses("-a");
for (unsigned int i = 0 ; i < num_flag_used ; i++) {
MArgList flag_arg_list;
stat = parser.getFlagArgumentList("-a", i, flag_arg_list);
if (stat != MS::kSuccess) {
MGlobal::displayError("Failed to get flag argument list : -a");
continue;
} else {
MString arg_str = flag_arg_list.asString(0, &stat);
if (stat == MS::kSuccess) {
// Do something...
}
}
}
}
}