Question

In: Computer Science

How do I add additional command line arguments in C++? I am working on a programming...

How do I add additional command line arguments in C++? I am working on a programming assignment that has the user input a file into the command line and then they have the option to also add a series of other arguments to the command line. I know how to accept the text file from the command line by using:

int main(int argc, char *argv[]) { /.../ }

Then filename(argv[1]) would be the text file that they put into the command line. The part I am confused how to do is the additional command line arguments and the syntax on how to use it. It is asking to add:

-LL=t which The line length should be set to t. The value of t must be a positive integer greater than 0.Default value of t = 20.

-IN=n which The indentation should be set to n. The value of n must be a positive integer greater than 0.Default value of n =5.

and there are a few more which I should be able to do once I understand how to add this option to add to the command line. Not sure how to add these arguments so they'll be accepted to the command line or how to set their default values. Also, do the dashes '-' make a difference in handling the additional commands?

Would I have to add something like ", int -LL=t)* or something like that after argv[], where I declared the main function?

Hopefully that made sense and I appreciate any help to understand how to do this.

Solutions

Expert Solution

As far as I can understand your question, below are my take on this:

Pro tip: Try to look into this more simply. These command-line arguments can be very complex if handled in the wrong way.

  1. We take the command line arguments - only two things matter - number of arguments and arguments itself.
  2. For simplicity, you can consider, the arguments which we get is simply an array of string and nothing else( In reality it's not a simple array of string, please keep this in mind).
  3. So providing -argument or argument doesn't matter in terms of arguments handling and conditional checking. These "-" are often used for visual benefits in simple terms.
  4. Also, for giving any argument any default value, simply treat the argv as a simple array of strings or integers and you will be good to go.

Please look at the comments of the program for more clarity.

#include<bits/stdc++.h>
using namespace std;

int main(int argc, char** argv)
{
    cout << "Total number of entered arguments: " << argc << "\n"; // argc will hold the number of command line arguments entered.

    /*
     Now for the default value which we want to have a default value

     Lets us suppose a, we are running the program as
     "multiple_command_line_arguments hey 1 -t a.txt".
     Here, for the third argument we want to have a default value as 5
    */
    int thirdValue = 5; // Setting the default value
    for (int i = 0; i < argc; ++i)  // Now here, we are looping through the entire given arguments.
        {
                                    // Our given arguments will start from index-1 and will go till index (argc-1)
            if(i==2 && argv[i]>0)   // argv[0] will have the name of the file
                cout << thirdValue << "\n";
            else
                cout << argv[i] << "\n";
        }

    return 0;
}

Sample Input-Output/Code-run:

Input: multiple_command_line_arguments hey 1 -t a.txt

Input: multiple_command_line_arguments hey 10 -t a.txt

Please let me know in the comments in case of any confusion. Also, please upvote if you like.


Related Solutions

I have written code in C programming that checks where the command line arguments are floats...
I have written code in C programming that checks where the command line arguments are floats or not. For example, if I type "./math 1 1 0 0 2.5 3" in the terminal, my program realizes they are all floats but if I type "./math 1 1 0 0 2.5 g", it recognizes that not all arguments are floats and gives an error message. I want to take my code further such that after typing in "./math 1 1 0...
In C programming using C11 library, how can we validate command line arguments? meaning for example...
In C programming using C11 library, how can we validate command line arguments? meaning for example if we input "./math 65 45 2.3 1 0 68" into the terminal, how can we validate that the numbers "65 45 2.3 1 0 68" are floats and not chars? In other terms the program should display and error message if the command line arguments are not floats and if they are floats, the program carries on as usual.
I am building a game in C programming language where I need to add objects of...
I am building a game in C programming language where I need to add objects of various length into a game board. The game board is 8X8 and we must account for the boundaries for the board and not go over them with our objects. The boards upper left corner is at 0x0 and we must return 1 if it fits and -1 if it does not fit. I have the following 2 functions to start with: ```int add_object_vert(int r,...
Hi, I am working on an assignment in C-Programming language dealing with LInked lists, in the...
Hi, I am working on an assignment in C-Programming language dealing with LInked lists, in the code there is instructions on where to write the code. I do not know how to write Linked Lists. Has to be in the C-language, Any help is greatly appreciated   //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void delink(void); void memexit(void); void wfile(void); /********************************************************************* this is the structure to hold a agent...
C# programming. Comment/Explain the below code line by line. I am having a hard time following...
C# programming. Comment/Explain the below code line by line. I am having a hard time following it. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Nth_prime {     class Program     {         public static bool isPrime(int number)         {             int counter = 0;             for (int j = 2; j < number; j++)             {                 if (number % j == 0)                 {                     counter = 1;                     break;                 }             }             if (counter == 0)             {                 return true;             }             else             {                 return false;             }         }...
Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
C programming in Shell Implement a MS-DOS style pipe command. Make sure it allows for command-line...
C programming in Shell Implement a MS-DOS style pipe command. Make sure it allows for command-line arguments to be passed to the programs. You only need to support one pipe command at a time. For example, when you type ls | wc the shell should write the output of ls to a temporary file by redirecting standard output when running ls, and run wc, redirecting standard input so it reads from the temporary file written to by ls.
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that...
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 1b) Write a C program that reads in a string from the keyboard. Use scanf with the conversion code %s. Recall that the 2nd arg in...
Please look at the following code. When I run it from the command line, I am...
Please look at the following code. When I run it from the command line, I am supposed to get the following results: 1: I am 1: I am I need to fix it so that the functions 'print_i' and 'print_j' print all of their lines. What do I need to add? Thank you. C source code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> // These two functions will run concurrently void* print_i(void *ptr) { printf("1: I am...
I am working as a cashier at Home Depot. The question is How do I use...
I am working as a cashier at Home Depot. The question is How do I use accounting in my life and my work? Does my work involve using financial accounting or managerial accounting reports?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT