简体   繁体   中英

Command line arguments in c#

I am pretty new to c# . Got a problem with command line arguments. what i want to do is make use of the third cmd line argument and write to it. I have specified the path of the file I want to write into and other stuffs. But the question here is can i access the command line arguments(for eg; args[3]) from user defined functions? How do we do tat? below is my code.

public class Nodes
{
public bool isVisited;
public string parent;
public string[] neighbour;
public int nodeValue;

public Nodes(string[] arr, int nodeValue)
{
    this.neighbour = new string[arr.Length];
    for (int x = 0; x < arr.Length; x++)
        this.neighbour[x] = arr[x];//hi...works??
    this.isVisited = false;
    this.nodeValue = nodeValue;
}


}

public class DFS
{
static List<string> traversedList = new List<string>();

static List<string> parentList = new List<string>();
static BufferBlock<Object> buffer = new BufferBlock<object>();
static BufferBlock<Object> buffer1 = new BufferBlock<object>();
static BufferBlock<Object> buffer3 = new BufferBlock<object>();
static BufferBlock<Object> buffer2 = new BufferBlock<object>();

public static void Main(string[] args)
{

    int N = 100;
    int M = N * 4;
    int P = N * 16;

    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();

    List<string> global_list = new List<string>();


    StreamReader file = new StreamReader(args[2]);

    string text = file.ReadToEnd();

    string[] lines = text.Split('\n');


    string[][] array1 = new string[lines.Length][];
    Nodes[] dfsNodes = new Nodes[lines.Length];

    for (int i = 0; i < lines.Length; i++)
    {
        lines[i] = lines[i].Trim();
        string[] words = lines[i].Split(' ');

        array1[i] = new string[words.Length];
        dfsNodes[i] = new Nodes(words, i);
        for (int j = 0; j < words.Length; j++)
        {
            array1[i][j] = words[j];
        }
    }
    StreamWriter sr = new StreamWriter(args[4]);

    int startNode = int.Parse(args[3]);

    if (args[1].Equals("a1"))
    {
        Console.WriteLine("algo 0");
        buffer.Post(1);
        dfs(dfsNodes, startNode, "root");
    }
    else if (args[1].Equals("a2"))
    {
        Console.WriteLine("algo 1");
        buffer1.Post(1);
        dfs1(dfsNodes, startNode, "root",sr);
    }
    else if (args[1].Equals("a3"))
    {
        buffer3.Post(1);
        List<string> visitedtList = new List<string>();
        Console.WriteLine("algo 2");
        dfs2(dfsNodes, startNode, "root", visitedtList,sr);
    }

    stopwatch.Stop();

    Console.WriteLine(stopwatch.Elapsed);
    Console.ReadLine();
}

public static void dfs(Nodes[] node, int value, string parent,StreamWriter sr1)
{
    int id = (int)buffer.Receive();
    sr1=new StreamWriter(arg
    Console.WriteLine("Node:" + value + " Parent:" + parent + " Id:" + id);
    sr1.Write("Node:" + value + " Parent:" + parent + " Id:" + id);
    id++;
    traversedList.Add(value.ToString());
    buffer.Post(id);
    for (int z = 1; z < node[value].neighbour.Length; z++)
    {
        if (!traversedList.Contains(node[value].neighbour[z]))
        {
            dfs(node, int.Parse(node[value].neighbour[z]), value.ToString(),sr1);
        }

    }
    return;



}

public static void dfs1(Nodes[] node, int value, string parent, StreamWriter sr)
{
    int id = (int)buffer1.Receive();
    sr.Write("Node:" + value + " Parent:" + parent + " Id:" + id);
    node[value].isVisited = true;
    node[value].parent = parent;
    id++;
    buffer1.Post(id);
    for (int z = 1; z < node[value].neighbour.Length; z++)
    {
        buffer2.Post(node[int.Parse(node[value].neighbour[z])]);
        if (!isVisited())
        {
            dfs1(node, int.Parse(node[value].neighbour[z]), value.ToString(),sr);
        }

    }
    return;



}

public static void dfs2(Nodes[] node, int value, string parent, List<string> visitedtList, StreamWriter sr)
{
    int id = (int)buffer3.Receive();
    sr.Write("Node:" + value + " Parent:" + parent + " Id:" + id);
    id++;
    visitedtList.Add(value.ToString());
    buffer3.Post(id);
    for (int z = 1; z < node[value].neighbour.Length; z++)
    {
        buffer2.Post(node[int.Parse(node[value].neighbour[z])]);
        if (!visitedtList.Contains(node[value].neighbour[z]))
            dfs2(node, int.Parse(node[value].neighbour[z]), value.ToString(), visitedtList,sr);

    }
    return;



}

public static bool isVisited()
{
    Nodes node = (Nodes)buffer2.Receive();
    return node.isVisited;
}

}

So the thing is I want to write the output of each dfs to the file specified as the command line argument. So can I have access to the args in the dfs, dfs1 methods??? Thank you.

您可以保留一个静态字段来保存它,也可以只使用Environment.GetCommandLineArgs()

Well, in its simplest form, just save it to use later

class Program
{
    static string _fpath;
    static void Main(string[] args)
    {
        // ...stuff
        _fpath = args[3];
    }

    static void WriteFile()
    {
        using(var stream = File.Open(_fpath, ...))
        {
            // write to file
        }
    }
}

Not necessarily exactly how I would do it, but you get the idea.

Also, regarding this bit of code...

this.neighbour = new string[arr.Length];
for (int x = 0; x < arr.Length; x++)
    this.neighbour[x] = arr[x];//hi...works??

You can simply write

this.neighbour = arr;

Ahh, the wonders of managed code :D. No need to copy elements across to the second array. Of course, you need to consider the fact that changes to elements in the argument array ( arr ) will be reflected in your internal array now.

It would be better to pass arguments into functions instead of relying on some "hidden" way to pass them.

Both static variable and GetCommandLineArgs are useful to pass them in hidden way (as pointed out in other answers). Drawbacks are harder to test (since need to set static shared dependency) and less clear for future readers that there is this hidden dependency.

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