コード例 #1
0
 internal ls(LSOpts opts)
     : this()
 {
     if (opts != null)
     {
         _opts = opts;
     }
 }
コード例 #2
0
 internal ls()
 {
     _opts    = new LSOpts();
     _entries = new List <LSFileInfo>();
 }
コード例 #3
0
        static void Main(string[] args)
        {
            bool   showUsage;
            int    exitCode = 0;
            ls     ls       = null;
            LSOpts opts;

#if NEW_ARGS
            CmdLineParameterCollection parms;
            string[] lsArgs;
#else
            bool bDefault = true;
            int  c;
#endif
            opts      = new LSOpts();
            showUsage = false;
            lsArgs    = null;
#if NEW_ARGS
            parms = new CmdLineParameterCollection(
                new ICmdLineParameter[] {
                new SwitchParameter('1', "single", "Generate output on a single line."),
                new SwitchParameter('a', "a", "\tcan't remember."),
                new SwitchParameter('d', "dirs", "Show directories in listing.", true),
                new SwitchParameter('l', "long", "Show long format."),
                new SwitchParameter('R', "recursive", "Descend subfolders."),

                new SwitchParameter('n', "sort-name", "Sort by name.", true),
                new SwitchParameter('s', "sort-size", "Sort by size."),
                new SwitchParameter('t', "sort-time", "Sort by date/time."),

                new SwitchParameter('h', "help", "This help message."),
                new SwitchParameter('?', "help", "This help message."),
            });
            if (args != null && args.Length > 0)
            {
                lsArgs = parms.decode(args);
                if (parms.errorFound)
                {
                    Console.Error.WriteLine(parms.errorMessages);
                    showUsage = true;
                    exitCode  = 1;
                }
                else
                {
                    if (parms["help"].isSelected)
                    {
                        Console.Error.WriteLine("help requested.");
                        showUsage = true;
                    }
                    else
                    {
                        if (parms["sort-size"].isSelected)
                        {
                            parms["sort-name"].isSelected = false;
                            parms["sort-time"].isSelected = false;
                            parms["long"].isSelected      = true;
                            parms["single"].isSelected    = true;
                        }
                        else if (parms["sort-time"].isSelected)
                        {
                            parms["sort-name"].isSelected = false;
                            parms["sort-size"].isSelected = false;
                            parms["long"].isSelected      = true;
                            parms["single"].isSelected    = true;
                        }
                        if (parms["sort-name"].isSelected)
                        {
                            opts.order = LSSortOrder.Name;
                        }
                        else if (parms["sort-size"].isSelected)
                        {
                            opts.order = LSSortOrder.Size;
                        }
                        else if (parms["sort-time"].isSelected)
                        {
                            opts.order = LSSortOrder.Time;
                        }
                        opts.singleLine      = parms["single"].isSelected;
                        opts.showDirectories = parms["dirs"].isSelected;
                        opts.longListing     = parms["long"].isSelected;
                        //						if (opts.longListing)
                        //							opts.singleLine=true;
                        opts.recursive = parms["recursive"].isSelected;
                    }
                }
            }
            if (showUsage)
            {
                usage(parms);
            }
            else
            {
                ls = new ls(opts);
                if (lsArgs != null && lsArgs.Length > 0)
                {
                    foreach (string anArg in lsArgs)
                    {
                        ls.findFiles(anArg);
                    }
                }
                else
                {
                    ls.findFiles(Path.Combine(Directory.GetCurrentDirectory(), "*.*"));
                }
                ls.showResults(Console.Out);
            }
#else
            if (args == null && args.Length > 0)
            {
                while ((c = GetOpt.getopt(args, "1adhlnst?")) >= 0)
                {
                    switch (c)
                    {
                    case '1': opts.singleLine = !opts.singleLine; break;

                    case 'a': break;

                    case 'd': opts.showDirectories = !opts.showDirectories; break;

                    case 'l': opts.longListing = !opts.longListing; break;

                    case 'n': opts.order = LSSortOrder.Name; break;

                    case 'R': opts.recursive = true; break;

                    case 's': opts.order = LSSortOrder.Size; break;

                    case 't': opts.order = LSSortOrder.Time; break;

                    case 'h':
                    case '?': showUsage = true;
                        break;

                    default:
                        Console.Error.WriteLine("unknown command-line flag '{0}'!", (char)c);
                        exitCode = 1;
                        break;
                    }
                }
                if (exitCode == 0 && !showUsage)
                {
                    ls = new ls(opts);
                    if (GetOpt.optind < args.Length)
                    {
                        for (int i = GetOpt.optind; i < args.Length; i++)
                        {
                            ls.findFiles(args[i]);
                        }
                        bDefault = false;
                    }
                }
            }
            if (!showUsage && exitCode == 0)
            {
                if (bDefault)
                {
                    if (ls == null)
                    {
                        ls = new ls(opts);
                    }
                    ls.findFiles(Directory.GetCurrentDirectory());
                }
            }
            if (ls != null)
            {
                ls.showResults(Console.Out);
            }
            if (showUsage)
            {
                usage();
            }
#endif
            Environment.Exit(exitCode);
        }