简体   繁体   中英

How does a program know which arguments are passed?

I'm new to the programming world.

I have a data extractor program which extracts data from MySQL into a word template file. I don't understand how the main program knows which argument to pass into the parameters. When running the exe, how does the program knows that "Haha, I need to pass a string in there, And it is precisely this string right here" ???.

EDIT: This program is supposed to be run when pressing a `Extraction Button' but how the exe program knows where is the string arg parameters? where does the exe get it from?

MAIN CODE

private static void Main(string[] args)
        {
            try
            {
                if (args[0] == "ALL" || args[0] == "*" || args[0] == "all")
                {
                    PrintARs();
                    Console.Read();
                }
                else
                {
                    CreateARDocument(args[0]);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.Read();
            }
        }

There are a number of ways:

  1. You don't. You just let people guess until they get it right.
  2. When invalid input, or no input, is provided the job could write out instructions to the console to help the user do it correctly
  3. You could provide external documentation, ie a readme.txt file.
  4. You could make the program interactive, rather than using command line arguments (write out text asking for what you want, and then get the input from standard input.
  5. You could make a real GUI for the program, with a textbox and a button for input. Usually when you see programs such as this (using command line args) it's because it's just a tool that isn't designed to really be user-friendly; it will be used by programmers who just know how to use it, or it will be used by other programs.

OK from your code heres some ideas

if your app was called app.exe

if you ran

app.exe 1 2 3 4 5

you would have args[] = { 1,2,3,4,5} and can look through them

if you ran

app.exe /all c:\my stuff\a.txt

you would get args[] = { /all, c:\\my, stuff\\a.txt }

Your code looks at the first arg and says is it "ALL, * or all" .. so Awkward people writing "All" would lose out.. and then sends that arg in which ever way they wrote it, to the funciton.

如果您从命令行执行此操作,则它将

myApp.exe "arg0" "arg1" "arg2"

static void Main(string[] args) is the entrance of the application.

args is the parameters passed in to Main

args.Length is the how many parameters you passed in

set a break point in your main function

right click your project in solution explorer -> click property -> click debug -> put abcd in Command line arguments, run the application in debug mode, and check the value of args

Inside ExtractButton click event:

System.Diagnostics.Process.Start("PATH to your exe file", "Command Line Arguments");

Order of your command arguments is the same as they comes in args[] array.

Those are the Command-Line Arguments .

You may have noticed that you always have a Program.cs file in your solution. That's where your Main() method is located. In a windows form project, it doesn't take a string[] argument, though you can change that, and then it runs your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM