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...
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,...
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;             }         }...
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...
This is my code for python. I am trying to do the fourth command in the...
This is my code for python. I am trying to do the fourth command in the menu which is to add an employee to directory with a new phone number. but I keep getting error saying , "TypeError: unsupported operand type(s) for +: 'dict' and 'dict". Below is my code. What am I doing wrong? from Lab_6.Employee import * def file_to_directory(File): myDirectory={}       with open(File,'r') as f: data=f.read().split('\n')    x=(len(data)) myDirectory = {} for line in range(0,199):      ...
IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your...
IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your program will create two files: even.txt - contains all integers from the input file that are even and greater than the threshold odd.txt - contains all integers from the input file that are odd and greater than the threshold The input file will exist and only contain a set of integers. It will always be valid data. Output whitespace will be ignored. Name the...
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?
Hello i am working on an assignment for my programming course in JAVA. The following is...
Hello i am working on an assignment for my programming course in JAVA. The following is the assignment: In main, first ask the user for their name, and read the name into a String variable. Then, using their name, ask for a temperature in farenheit, and read that value in. Calculate and print the equivalent celsius, with output something like Bob, your 32 degrees farenheit would be 0 degrees celsius Look up the celsius to farenheit conversion if you do...
Hello, I am working on an assignment but I am unsure of how to solve it....
Hello, I am working on an assignment but I am unsure of how to solve it. Please help me. The assignment details are below. Consider this scenario: Your friend starts a website, nothingbutflags.com, which is not making money. Your friend asks you to help generate more traffic. You ask your friend how much traffic the website had last month? And your friend replies and says only 500 visits. You also ask how many flags did you sell? Your friend replies...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT