0

I need help to display name to command line like this (I don't know how to explain) in C

$:Enter your name: Test
$:Test>

But when you continue press enter it still showing Test>

$:Test>
$:Test>

So how do we get argv[0] and do something like this (Sorry that I cannot explain probably)

Thank you

5
  • 1
    I answered a prior question regarding how to get command line arguments here: stackoverflow.com/questions/18937861/…. See if that helps get you started. Commented Oct 3, 2013 at 1:58
  • Are you trying to write an interactive program? And a way to set the prompt for that program? Commented Oct 3, 2013 at 2:04
  • 1
    Or are you trying to rename the command line prompt? Commented Oct 3, 2013 at 2:05
  • can you provide a use case where argv[0] is actually used, currently it looks like you are looking for a fgets loop and would need to interpret the input Commented Oct 3, 2013 at 2:26
  • Are you asking how to change the primary prompt of a standard shell (such as bash), or how to change the prompt in a shell you're writing, or something else? I really can't work out what you're trying to do from your description. Commented Oct 3, 2013 at 2:40

3 Answers 3

2

command line arguments are stored in char **argv, and there are argc of them.

int main(int argc, char **argv)
{
    int i=0;
    for(i=0; i< argc; i++)
       printf("argument number %d = %s\n", i, argv[i]);
    return 0;
}

argv[0] is the name of the program being executed, so argc is always at least == 1 ( or more)

Sign up to request clarification or add additional context in comments.

1 Comment

You forgot to declare the type of the argc parameter. So your example won't compile. ;)
0

If you had rather shell-like program in mind, maybe the following couldbe of use:

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define BUFSIZE 64

int main() {
  char prompt[BUFSIZE];
  char command[BUFSIZE];
  char *prefix = "$:";
  char *suffix = ">";

  printf("%s%s%s", prefix, "Enter your name:", suffix);
  fgets(prompt, BUFSIZE, stdin);
  prompt[strlen(prompt)-1] = '\0'; // get rid of the \n

  while (true) {
    printf("%s%s%s", prefix, prompt, suffix);
    fgets(command, BUFSIZE, stdin);
    if (strncmp(command,"Quit",4) == 0)
      break;
  }

  return 0;
}

Comments

0

Whenever possible, you should use getopt() so that the order of your parameters doesn't matter. For example, suppose you wanted to take an integer parameter for the size, an integer for the mode of execution, and a toggle to indicate whether to run in "quiet mode" or not. Further suppose that "-h" should print help and exit. Code like this will do the trick. The "s:m:hq" string indicates that "-s" and "-m" provide parameters, but the other flags don't.

int main() {
  // parse the command-line options
  int opt;
  int size = DEFAULT_SIZE, mode = DEFAULT_MODE, quiet = 0;
  while ((opt = getopt(argc, argv, "s:m:hq")) != -1) {
    switch (opt) {
      case 's': size  = atoi(optarg); break;
      case 'm': mode  = atoi(optarg); break;
      case 'q': quiet = 1;            break;
      case 'h': usage(); return 0;
    }
  }
  // rest of code goes here
}

Of course, you should add error checking in case optarg is null.

Also, if you're using C++, "string(optarg)" is an appropriate way for your case statement to set a std::string to hold a value that is stored as a char* in argv.

1 Comment

i think the question is rather about parsing user input for a shell-like interactive program

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.