示例#1
0
        /// <exception cref="System.Exception"/>
        public static void TestCommandFormat()
        {
            // This should go to TestFsShell.java when it is added.
            CommandFormat cf;

            cf = new CommandFormat("copyToLocal", 2, 2, "crc", "ignoreCrc");
            NUnit.Framework.Assert.AreEqual(cf.Parse(new string[] { "-get", "file", "-" }, 1)
                                            [1], "-");
            try
            {
                cf.Parse(new string[] { "-get", "file", "-ignoreCrc", "/foo" }, 1);
                Fail("Expected parsing to fail as it should stop at first non-option");
            }
            catch (Exception)
            {
            }
            // Expected
            cf = new CommandFormat("tail", 1, 1, "f");
            NUnit.Framework.Assert.AreEqual(cf.Parse(new string[] { "-tail", "fileName" }, 1)
                                            [0], "fileName");
            NUnit.Framework.Assert.AreEqual(cf.Parse(new string[] { "-tail", "-f", "fileName" }, 1)[0], "fileName");
            cf = new CommandFormat("setrep", 2, 2, "R", "w");
            NUnit.Framework.Assert.AreEqual(cf.Parse(new string[] { "-setrep", "-R", "2", "/foo/bar" }, 1)[1], "/foo/bar");
            cf = new CommandFormat("put", 2, 10000);
            NUnit.Framework.Assert.AreEqual(cf.Parse(new string[] { "-put", "-", "dest" }, 1)
                                            [1], "dest");
        }
示例#2
0
            /// <exception cref="System.IO.IOException"/>
            protected internal override void ProcessOptions(List <string> args)
            {
                CommandFormat cf = new CommandFormat(1, int.MaxValue, "ignoreCrc");

                cf.Parse(args);
                verifyChecksum = !cf.GetOpt("ignoreCrc");
            }
示例#3
0
            // used by chown/chgrp
            ///allows only "allowedChars" above in names for owner and group
            /// <exception cref="System.IO.IOException"/>
            protected internal override void ProcessOptions(List <string> args)
            {
                CommandFormat cf = new CommandFormat(2, int.MaxValue, "R");

                cf.Parse(args);
                SetRecursive(cf.GetOpt("R"));
                ParseOwnerGroup(args.RemoveFirst());
            }
示例#4
0
        /// <exception cref="System.IO.IOException"/>
        protected internal override void ProcessOptions(List <string> args)
        {
            CommandFormat cf = new CommandFormat(1, int.MaxValue, OptionFollowLink, OptionFollowArgLink
                                                 , null);

            cf.Parse(args);
            if (cf.GetOpt(OptionFollowLink))
            {
                GetOptions().SetFollowLink(true);
            }
            else
            {
                if (cf.GetOpt(OptionFollowArgLink))
                {
                    GetOptions().SetFollowArgLink(true);
                }
            }
            // search for first non-path argument (ie starts with a "-") and capture and
            // remove the remaining arguments as expressions
            List <string>        expressionArgs = new List <string>();
            IEnumerator <string> it             = args.GetEnumerator();
            bool isPath = true;

            while (it.HasNext())
            {
                string arg = it.Next();
                if (isPath)
                {
                    if (arg.StartsWith("-"))
                    {
                        isPath = false;
                    }
                }
                if (!isPath)
                {
                    expressionArgs.AddItem(arg);
                    it.Remove();
                }
            }
            if (args.IsEmpty())
            {
                args.AddItem(Path.CurDir);
            }
            Expression expression = ParseExpression(expressionArgs);

            if (!expression.IsAction())
            {
                Expression         and      = GetExpression(typeof(And));
                Deque <Expression> children = new List <Expression>();
                children.AddItem(GetExpression(typeof(Print)));
                children.AddItem(expression);
                and.AddChildren(children);
                expression = and;
            }
            SetRootExpression(expression);
        }
示例#5
0
            /// <exception cref="System.IO.IOException"/>
            protected internal override void ProcessOptions(List <string> args)
            {
                CommandFormat cf = new CommandFormat(2, int.MaxValue, "R", null);

                cf.Parse(args);
                SetRecursive(cf.GetOpt("R"));
                string modeStr = args.RemoveFirst();

                try
                {
                    pp = new ChmodParser(modeStr);
                }
                catch (ArgumentException)
                {
                    // TODO: remove "chmod : " so it's not doubled up in output, but it's
                    // here for backwards compatibility...
                    throw new ArgumentException("chmod : mode '" + modeStr + "' does not match the expected pattern."
                                                );
                }
            }
示例#6
0
        /// <exception cref="System.IO.IOException"/>
        protected internal override void ProcessOptions(LinkedList <string> args)
        {
            CommandFormat cf = new CommandFormat(2, int.MaxValue, "w");

            cf.Parse(new List <string>(args));
            waitOpt = cf.GetOpt("w");
            try
            {
                newLength = long.Parse(args.First.Value);
                args.RemoveFirst();
            }
            catch (FormatException nfe)
            {
                DisplayWarning("Illegal length, a non-negative integer expected");
                throw;
            }
            if (newLength < 0)
            {
                throw new ArgumentException("length must be >= 0");
            }
        }
示例#7
0
 public virtual void TestOldArgsWithIndex()
 {
     string[] arrayArgs = new string[] { "ignore", "-a", "b", "-c" };
     {
         CommandFormat  cf         = new CommandFormat(0, 9, "a", "c");
         IList <string> parsedArgs = cf.Parse(arrayArgs, 0);
         Assert.Equal(SetOf(), cf.GetOpts());
         Assert.Equal(ListOf("ignore", "-a", "b", "-c"), parsedArgs);
     }
     {
         CommandFormat  cf         = new CommandFormat(0, 9, "a", "c");
         IList <string> parsedArgs = cf.Parse(arrayArgs, 1);
         Assert.Equal(SetOf("a"), cf.GetOpts());
         Assert.Equal(ListOf("b", "-c"), parsedArgs);
     }
     {
         CommandFormat  cf         = new CommandFormat(0, 9, "a", "c");
         IList <string> parsedArgs = cf.Parse(arrayArgs, 2);
         Assert.Equal(SetOf(), cf.GetOpts());
         Assert.Equal(ListOf("b", "-c"), parsedArgs);
     }
 }
示例#8
0
        private static CommandFormat CheckArgLimits <T>(Type expectedErr, int min, int max
                                                        , params string[] opts)
        {
            CommandFormat  cf         = new CommandFormat(min, max, opts);
            IList <string> parsedArgs = new AList <string>(args);
            Type           cfError    = null;

            try
            {
                cf.Parse(parsedArgs);
            }
            catch (ArgumentException e)
            {
                System.Console.Out.WriteLine(e.Message);
                cfError = e.GetType();
            }
            Assert.Equal(expectedErr, cfError);
            if (expectedErr == null)
            {
                Assert.Equal(expectedArgs, parsedArgs);
                Assert.Equal(expectedOpts, cf.GetOpts());
            }
            return(cf);
        }
示例#9
0
        /// <summary>Main entry point.</summary>
        /// <param name="args">command-line arguments</param>
        public static void Main(string[] args)
        {
            if (args.Length < 1 || args[0].Equals("-h") || args[0].Equals("--help"))
            {
                System.Console.Out.WriteLine(usage);
                return;
            }
            // Copy args, because CommandFormat mutates the list.
            IList <string> argsList = new AList <string>(Arrays.AsList(args));
            CommandFormat  cf       = new CommandFormat(0, int.MaxValue, "-glob", "-jar");

            try
            {
                cf.Parse(argsList);
            }
            catch (CommandFormat.UnknownOptionException)
            {
                Terminate(1, "unrecognized option");
                return;
            }
            string classPath = Runtime.GetProperty("java.class.path");

            if (cf.GetOpt("-glob"))
            {
                // The classpath returned from the property has been globbed already.
                System.Console.Out.WriteLine(classPath);
            }
            else
            {
                if (cf.GetOpt("-jar"))
                {
                    if (argsList.IsEmpty() || argsList[0] == null || argsList[0].IsEmpty())
                    {
                        Terminate(1, "-jar option requires path of jar file to write");
                        return;
                    }
                    // Write the classpath into the manifest of a temporary jar file.
                    Path   workingDir = new Path(Runtime.GetProperty("user.dir"));
                    string tmpJarPath;
                    try
                    {
                        tmpJarPath = FileUtil.CreateJarWithClassPath(classPath, workingDir, Runtime.GetEnv
                                                                         ())[0];
                    }
                    catch (IOException e)
                    {
                        Terminate(1, "I/O error creating jar: " + e.Message);
                        return;
                    }
                    // Rename the temporary file to its final location.
                    string jarPath = argsList[0];
                    try
                    {
                        FileUtil.ReplaceFile(new FilePath(tmpJarPath), new FilePath(jarPath));
                    }
                    catch (IOException e)
                    {
                        Terminate(1, "I/O error renaming jar temporary file to path: " + e.Message);
                        return;
                    }
                }
            }
        }