如何在winform C#的命令行参数中使用标志 [英] How do I use flags in command line argument for winform C#

查看:90
本文介绍了如何在winform C#的命令行参数中使用标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#类将创建一个GUI,它接收csv文件并相应地绘制折线图(参见: C#从CSV文件读取字符串并绘制图表代码的线图 [ ^ ])。目前,我的图表是使用GUI文件对话框绘制和保存的。



我现在要做的是读取,绘图和保存使用命令行代替图形(无需GUI),例如graphdemo.exe -fc:\\\\\\\\\ .cv(csv文件路径)-oc:\\ desktop \\1.png(输出图形文件路径)。



我可以知道如何调用我的Read和Plot类使用-f标志并使用-o标志保存绘制的图形?



谢谢。



我尝试过:



My C# classes will create a GUI that takes in csv files and plot a line graph accordingly (Refer to: C# Read string from CSV file and plot line graph for graph code[^]). Currently, my graphs are plotted and saved using the GUI file dialog.

What I am trying to do now is to read, plot and save the graph using command-line instead (NO GUI needed), such as "graphdemo.exe -f c:\\desktop\\1.csv (csv file path) -o c:\\desktop\\1.png (output graph file path)".

May I know how could I call my Read and Plot classes using the "-f" flag and save the plotted graph using the "-o" flag ?

Thanks.

What I have tried:

class Program
    {
        [STAThread]
        public static void Main(string[] args)
        {           
            if (args.Length > 0)
            {
                if (args.Contains("-f")) //-f: Read user-input csv file path and plot line graph
                {
                    for (int i = 1; i < args.Length; i++)
                    {
                        string filepath = args[i]; //Store each file path into an array

                        if (File.Exists(filepath))
                        {
                            string filename = Path.GetFileName(filepath); //Get file name
                            //read csv file from Read class
                            //plot line graph from Plot class

                            Console.WriteLine(filename);
                        }
                        else
                        {
                            Console.WriteLine("\nPlease enter a valid file name. ");
                        }
                    }
                }
                if (args.Contains("-o"))    
                {
                    // -o: Save plotted graph as png image based on user-input path
                }

                else
                {
                    Console.WriteLine("\nPlease enter a valid argument. ");
                }
            }
            else 
            {
                Application.Run(new GraphDemo());   // Show graph GUI
            }
        }
    }
}

推荐答案

那是不是一个好主意:你的代码存在很多问题。

琐碎的东西:

That's not a good idea: there are quite a few problems with your code here.
Trivial stuff:
string filepath = args[i]; //Store each file path into an array

不,它没有。错误的评论比没有评论要糟糕得多。

主要的问题是你没有考虑如果用户犯了错误会发生什么:如果他以错误的顺序键入参数怎么办?

虽然可以使用-f和-o,但正常的默认值是

No, it doesn't. And a wrong comment is much, much worse than no comment.
The main problem is that you aren't considering what happens if the user makes a mistake: what if he types parameters in the wrong order.
While it's ok to use -f and -o, the normal default is

exename infile {outfile} {parameters}

参数以' - '或'/'开头标记。

我处理它的方式是预设:

Where parameters are marked by starting with either a '-' or a '/'.
The way I handle it is to preset:

string inpath = LoadLastInputFile();
string outpath = inpath;
bool firstFile = true;
foreach (string arg in args)
   {
   if (arg.StartsWith('/') || arg.StartsWith('-'))
      {
      HandleOption(arg);
      }
   else
      {
      if (firstFile)
         {
         if (!File.Exists(arg))
             {
             Console.WriteLine("Input file not found: {0}", arg);
             return;
             }
         inpath = arg;
         outpath = inpath;
         firstFile = false;
         }
      else
         {
         outpath = arg;
         }
      }
   }


参见 Environment.GetCommandLineArgs Method(System) [ ^ ]。


这篇关于如何在winform C#的命令行参数中使用标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆