// in an previous version white space was allowed befor the command, but not now. // a command string always starts at postion 2 and ends at the first white space // the command looks like an MQTT topic or the directory structure of a file system // e.g. /0/pwm 127 // find end of command and place a null termination so it can be used as a string uint8_t findCommand(void) { uint8_t lastAlpha =2; // the command always starts after the addrss at position 2 command = command_buf + lastAlpha; // Only an isspace or null may terminate a valid command. // The command is made of chars of isalpa, '/', or '?'. while( !( isspace(command_buf[lastAlpha]) || (command_buf[lastAlpha] == '\0') ) && lastAlpha < (COMMAND_BUFFER_SIZE-1) ) { if ( isalpha(command_buf[lastAlpha]) || (command_buf[lastAlpha] == '/') || (command_buf[lastAlpha] == '?') ) { lastAlpha++; } else { if (echo_on) printf_P(PSTR("{\"err\": \"BadCharInCmd '%c'\"}\r\n"),command_buf[lastAlpha]); initCommandBuffer(); return 0; } } // command does not fit in buffer if ( lastAlpha >= (COMMAND_BUFFER_SIZE-1) ) { if (echo_on) printf_P(PSTR("{\"err\": \"HugeCmd\"}\r\n")); initCommandBuffer(); return 0; } if ( isspace(command_buf[lastAlpha]) ) { // the next poistion may be an argument. if ( findArgument(lastAlpha+1) ) { // replace the space with a null so command works as a null terminated string. command_buf[lastAlpha] = '\0'; } else { // isspace() found after command but argument was not valid if (echo_on) printf_P(PSTR("{\"err\": \"CharAftrCmdBad '%c'\"}\r\n"),command_buf[lastAlpha+1]); initCommandBuffer(); return 0; } } else { if (command_buf[lastAlpha] != '\0') { // null must end command. if (echo_on) printf_P(PSTR("{\"err\": \"MissNullAftrCmd '%c'\"}\r\n"),command_buf[lastAlpha]); initCommandBuffer(); return 0; } } // zero indexing is also the count and should match with strlen() return lastAlpha; }
//============================================================================== int performCommandLine (const String& commandLine) { StringArray args; args.addTokens (commandLine, true); args.trim(); if (findArgument (args, "--lf") || findArgument (args, "-lf")) preferredLinefeed = "\n"; String command (args[0]); try { if (matchArgument (command, "help")) { showHelp(); return 0; } if (matchArgument (command, "h")) { showHelp(); return 0; } if (matchArgument (command, "resave")) { resaveProject (args, false); return 0; } if (matchArgument (command, "resave-resources")) { resaveProject (args, true); return 0; } if (matchArgument (command, "get-version")) { getVersion (args); return 0; } if (matchArgument (command, "set-version")) { setVersion (args); return 0; } if (matchArgument (command, "bump-version")) { bumpVersion (args); return 0; } if (matchArgument (command, "git-tag-version")) { gitTag (args); return 0; } if (matchArgument (command, "buildmodule")) { buildModules (args, false); return 0; } if (matchArgument (command, "buildallmodules")) { buildModules (args, true); return 0; } if (matchArgument (command, "status")) { showStatus (args); return 0; } if (matchArgument (command, "trim-whitespace")) { cleanWhitespace (args, false); return 0; } if (matchArgument (command, "remove-tabs")) { cleanWhitespace (args, true); return 0; } if (matchArgument (command, "tidy-divider-comments")) { tidyDividerComments (args); return 0; } if (matchArgument (command, "fix-broken-include-paths")) { fixRelativeIncludePaths (args); return 0; } if (matchArgument (command, "obfuscated-string-code")) { generateObfuscatedStringCode (args); return 0; } if (matchArgument (command, "encode-binary")) { encodeBinary (args); return 0; } if (matchArgument (command, "trans")) { scanFoldersForTranslationFiles (args); return 0; } if (matchArgument (command, "trans-finish")) { createFinishedTranslationFile (args); return 0; } if (matchArgument (command, "set-global-search-path")) { setGlobalPath (args); return 0; } if (matchArgument (command, "create-project-from-pip")) { createProjectFromPIP (args); return 0; } } catch (const CommandLineError& error) { std::cout << error.message << std::endl << std::endl; return 1; } return commandLineNotPerformed; }
void solve() { int ans = 0; memset(matched, 0, sizeof(matched)); for (int i = 1; i <= n; i++) if (!matched[i] && findArgument(i)) // find argument road ans++; printf("%d\n", 2*ans); for (int i = 1; i <= n; i++) if (matched[i]) if (i < matched[i]) printf("%d %d\n", i, matched[i]); }
// // Arguments::has // -------------- // Cycle through the tokens of argument and return true if any of them // are found. // bool Arguments::has(const std::string& argument) const throw(std::bad_alloc) { return findArgument(argument) != -1; }
inline void QtArgCmdLine::parse( bool parseAfterIgnoreRest ) { checkArgumentsCorrectness(); QString lastArgNameOrFlag; while( !m_context.atEnd() ) { const QString & arg = m_context.next(); QString name, nameWithoutDelims; { QString value; splitArgumentAndValue( arg, name, value ); nameWithoutDelims = removeDelimiters( name ); if( !value.isNull() ) m_context.prepend( value ); } // Argument. if( isArgument( arg ) ) { if( nameWithoutDelims.isEmpty() ) { if( !parseAfterIgnoreRest ) return; else continue; } QtArgIface * argument = findArgument( nameWithoutDelims ); lastArgNameOrFlag = name; if( argument != NULL ) { argument->process( m_context ); argument->visit( m_context ); argument->checkConstraint(); } else throw QtArgUnknownArgumentEx( QString::fromLatin1( "Unknown argument: %1" ) .arg( name ) ); } // Flag. else if( isFlag( arg ) ) { for( int i = 0, i_max = nameWithoutDelims.length() - 1; i <= i_max; ++i ) { QtArgIface * argument = findArgument( nameWithoutDelims[ i ] ); lastArgNameOrFlag = QString( m_delimiter ) + nameWithoutDelims[ i ]; if( i != i_max ) { QStringList dummyArgsList( nameWithoutDelims .right( nameWithoutDelims.length() - i - 1 ) ); QtArgCmdLineContext dummyContext( dummyArgsList ); if( argument->process( dummyContext ) ) { argument->visit( m_context ); argument->checkConstraint(); break; } } else argument->process( m_context ); argument->visit( m_context ); argument->checkConstraint(); } } // Something unexpected. else throw QtArgUnexpectedOptionEx( QString::fromLatin1( "Unexpected option: %1. " "Argument \"%2\" doesn't expect any values." ) .arg( name ).arg( lastArgNameOrFlag ) ); } checkMandatoryArguments(); }