c++ - How to create commands with variables to be executed in cmd - c++

Let me specify the question. I want to make commands so when user will type eg. go (something) it will use the go to specify the function and something to find what the program should do when variable equals something. If there is anything unclear, just ask, i know my explanation is strange.
Example:
In program there is a void go(string choice) function with if condition including few variables that can be used through choice string.
User is opening program and typing go and then string. Program goes to function go and if string is included in if loop, then program goes to specific if condition and does what it should eg.:
if(choice == "room")
{
//condition being executed
}

So basically you want a specific function to be executed according to a cmd line input? Easy! Create you main function like this:
int main(int argc, char *argv[])
argc is the argument count and argv is the argument value. Google up on this if you need more info, it's widely used. Use an if statement inside your main function that will call the required function according to the value of argv[]. So when you execute your program you'll execute like
exe_file_name go anything
If you want to call multiple functions dynamically during a single execution you could instead do
getline(cin,choice);
if(strcmp(choice, "room")) //condition

I think it should be like
if(strcmp(choice , "room")){//condition being executed}

Related

How to make a console app that excutes a command only when called

I am working on a project that has some functionality similar to windows task manager but as a console app. So far I managed to make the app present menus and, according to the user's choice, the app runs a specific functionality.
Now, I am thinking of letting the app do some function using commands for example, let's say we have the command close -all. This is supposed to close all the open windows.
In fact, I have some information about how I can process the command and call the corresponding function, I think using a lexer to produce tokens and then use a parser to call the functions, please correct me if I am mistaken.
However I can use the commands only if my application is running in cmd. what I want is to call a command like wg close all (wg being the app name) at any time and then maybe continue running other cmd commands like mkdir or cd.
This sounds like you need the basic functionality of a C++ program which is provided by the main(int argc, char* argv[]) method (refer to this documentation if needed: https://en.cppreference.com/w/cpp/language/main_function).
With argc being the number of arguments and argv being an array with the values of arguments. I assume that your application builds up a GUI with Qt or some library, which you would not need to fire up if ran in batch mode.
Depending on the complexity of the arguments you need to handle, you might do the trick with a very basic structure to parse the arguments.
Example Pseudo-Code
main(argc, argv) {
if (argc == 0) {
startProgramInNormalGUIMode();
exit(0);
}
// This could be a sub method "parse(argc, argv)"
String parameter="";
Boolean executed=false;
for (i=0; i < argc; ++i) {
if element == "close" {
set parameter = argv[i+1];
i++; // skip next element which was the parameter
// myCloseOperation would check if parameter is
// "all" for example and closes all windows
// or treat any other value as window title for
// example and closes only the matching window.
myCloseOperation(parameter);
set executed = true;
}
if element == "operation2" {
set parameter = argv[i+1];
i++;
myOperation2Method(parameter);
set executed = true;
}
}
if (executed == false) {
showSyntaxErrorMessage();
exit(1);
}
exit(0);
}
The pseudo code above assumes that every operation has an identifying name and only one parameter following it. You could alter the code to also allow zero parameters or more than one and change the i incrementation accordingly.
You could also add a check if a parameter or operation string starts with a dash or slash or some other prefix you want.
Also it is assumed that an operation and its parameter(s) may occur more than once in the command line and every occurance would be executed. For example wg close aWindowTitle close anotherWindowTitle close all would be parsed as valid and three close operations would be performed.

VideoCapture with " int main(int argc, char **argv) "

I'm trying to use VideoCapture. a part of my code is below.
when I run my code, I got this :
Error! Insufficient parameters provided.
How can I use my video with this code. I want to open a stream with VLC.
Or if is there any other way, I'd like to use.
I searched that argv[1] is will be my video file. But I don't know how to show my file and how to define my file to this code.
To help future users, I'd make some changes:
Was:
LOG_DEBUG("Error! Insufficient parameters provided.");
Is:
std::string program(argv[0]);
LOG_DEBUG("Error! Insufficient parameters provided.");
LOG_DEBUG("Please provide a command line argument.");
LOG_DEBUG("Example: " << program << " VIDEO_FILE_NAME");
Explanation:
On the command line, when the program is invoked, the arguments from the command line are copied in the array of strings held by argv. argv[0] is the first argument and it is the filename of the program itself. Put another way, argv[i] for 0 <= i < argc are populated in the array of strings argv from the command line. If you renamed the program executable file, argv[0] would be different the next time you ran the program.
The array argv is indexed from 0 to argc-1. When main is invoked this array of strings and argc are set. It's up to the software to decide what to do. In this case your application tests argc and finds that if no argument is provided (ie., argc < 2) then the one user argument provided by the user is not present, report the error and return.
Incidentally, there's yet another form of main you can use:
int main(int argc, char** argv, char** envp)
argc = number of arguments.
argv = array of argument strings
envp = array of environment variable name=value pairs
So beyond simple command line argument passing, one could choose to write the main function to grab the environment variables (not shell variables) and decide nuanced action based on that. Options abound.
But for the time being, your code would be helpful if it reported why there is an error and the suggestions provided seem to do that.
Good luck.

passing on inputs to an exe file using c++

lets say we have a cal.exe file (a simple addition calculator programmed programmed in c++).
lets say that the console output screen first displays enter the first number: and waits for the user to input an integer value. I am willing to create a c++ program that would "pass on" the required value to the running process (cal.exe) as an input (playing the role of a user). I would also like to have the output from the cal.exe file to be displayed and interpreted by my program.
I havent got the slightest idea how to proceed with this. Is there any open source library that would help me accomplish this? If there is, could you name a few?
I have just learned object oriented programming in c++ last year in my school and I am not used to these kind of stuff in programming; so please excuse me if this question is silly.
update:
lets consider 2 processes a.exe and b.exe running. could you tell me a possible way to program b.exe which provides a integer input to a.exe (a console process) as if it was from the user?
You can do this by accepting command line arguments.something like this
int main ( int argc, char *argv[] )
{
enter code here
return 0;
}
Where,
First argument to main function (argc) refers to the number of arguments being passed to the program at run-time.
Second (char *argv[] )refers to a string containing the arguments that are passed (char * is treated as String also ).
Argument names may vary as per the user specifications.
For details Refer:
http://www.cplusplus.com/articles/DEN36Up4/
And for nesting of programs you can use system("name of child program goes here") Function under stdlib.h.
For details Refer:
http://www.cplusplus.com/reference/cstdlib/system/

Can I change the binding of RET in gdb?

I'd like to disable the gdb behavior where typing a carriage return repeats execution of the last command entered. I'd just like it to do nothing. Is this possible?
It seems that repeating most commmands is a default gdb's behavior and there is no setting to change it. This is how it looks in gdb's source:
/* Handle a complete line of input. This is called by the callback
mechanism within the readline library. Deal with incomplete
commands as well, by saving the partial input in a global
buffer. */
static void
command_line_handler (char *rl)
{
...
int repeat = (instream == stdin);
So as you can see repeat is assigned 1 if instream is STDIN. There is no other way to assign repeat a different value.
So what you can do is to build your own gdb executable on your machine from gdb`s source (http://ftp.gnu.org/gnu/gdb/). But before building change a little the line 591 in gdb/event-top.c. Instead of
int repeat = (instream == stdin);
write
int repeat = 0;
One possible trick that might work -- I didn't try it -- would be to use Python to set the prompt callback to invoke "dont-repeat".
It seems like a reasonable feature request to me that gdb have a setting to disable command repetition.

When opening a program in a Unix terminal, is there a way to have the program name and its input on the same line?

I have a C++ program that accepts three inputs: an integer for width, an integer for height, and a filename. Right now, I compile, and run the program like this (assuming I named it prog):
>prog
// hit enter
>128 128 output.ppm
This results in a successful output, but the program description says the proper command-line syntax is:
>prog w h filename
That's all it says. Does this imply that my program should be able to start on the same line? Perhaps it implicitly means you hit enter after typing the program name, but if not, is there a way to actually do this?
Your program needs to parse command line parameters. Looking at the specification, the expected workflow is
>prog 128 128 output.ppm
//hit enter after the parameters
Look here to learn more.
You're approaching the problem incorrectly. You are taking your input via std::cin after your program has been started. Your program specification states that the input should be given as part of the command. Consider a command like ls -l - the -l is part of the command and is passed to the program to parse and act upon.
You need to allow a command like prog 128 128 output.ppm to be run, so the user would type that and then press enter to run the program. How do you get access to the command line arguments within your C++ program? Well, that's what the argc and argv parameters of the main function are for. Your main function should look like this:
int main(int argc, char* argv[]) { ... }
The argc argument gives you the number of arguments passed in the command line (it will be 4, in the example given) which is also the size of the argv array. Each element is an argument from the command. For example, argv[0] will be "prog", argv[1] will be "128", and so on. You need to parse these values and, depending on their values, change the functionality of your program.
You can pass command via the argument in the main function:
int main(int argc, char *argv[]) {
}
argc is the number of arguments and argv is an array of arguments.