将包含命令行参数的字符串拆分为Java中的String [] [英] Split a string containing command-line parameters into a String[] in Java

查看:130
本文介绍了将包含命令行参数的字符串拆分为Java中的String []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于 C#的此主题,我需要将包含命令行参数的字符串拆分到我的程序中,这样我就可以让用户轻松运行多个命令。例如,我可能有以下字符串:

Similar to this thread for C#, I need to split a string containing the command line arguments to my program so I can allow users to easily run multiple commands. For example, I might have the following string:

-p /path -d "here's my description" --verbose other args

鉴于上述情况,Java通常会将以下内容传递给main:

Given the above, Java would normally pass the following in to main:

Array[0] = -p
Array[1] = /path
Array[2] = -d
Array[3] = here's my description
Array[4] = --verbose
Array[5] = other
Array[6] = args

我不需要担心任何shell扩展,但它必须足够聪明才能处理单引号和双引号以及可能存在的任何转义在字符串中。有没有人知道在这些条件下shell会解析字符串的方法?

I don't need to worry about any shell expansion, but it must be smart enough to handle single and double quotes and any escapes that may be present within the string. Does anybody know of a way to parse the string as the shell would under these conditions?

注意:我做 NOT 需要进行命令行解析,我已经使用 joptsimple 来做到这一点。相反,我想让我的程序易于编写脚本。例如,我希望用户能够在一个文件中放置一组命令,每个命令在命令行上都是有效的。例如,他们可能会在文件中键入以下内容:

NOTE: I do NOT need to do command line parsing, I'm already using joptsimple to do that. Rather, I want to make my program easily scriptable. For example, I want the user to be able to place within a single file a set of commands that each of which would be valid on the command line. For example, they might type the following into a file:

--addUser admin --password Admin --roles administrator,editor,reviewer,auditor
--addUser editor --password Editor --roles editor
--addUser reviewer --password Reviewer --roles reviewer
--addUser auditor --password Auditor --roles auditor

然后用户将运行我的管理工具,如下所示:

Then the user would run my admin tool as follows:

adminTool --script /path/to/above/file

main()然后会找到 - script 选项并迭代不同的行在文件中,将每一行拆分为一个数组,然后我将在一个joptsimple实例中反射,然后将其传递给我的应用程序驱动程序。

main() will then find the --script option and iterate over the different lines in the file, splitting each line into an array that I would then fire back at a joptsimple instance which would then be passed into my application driver.

joptsimple附带一个Parser有一个解析方法,但它只支持排序 String 数组。同样, GetOpt 构造函数也需要字符串[] - 因此需要解析器。

joptsimple comes with a Parser that has a parse method, but it only supports a String array. Similarly, the GetOpt constructors also require a String[] -- hence the need for a parser.

推荐答案

这是一个非常简单的替代方案将文本行从文件拆分为参数向量,以便将其提供给选项解析器:

Here is a pretty easy alternative for splitting a text line from a file into an argument vector so that you can feed it into your options parser:

这是解决方案:

public static void main(String[] args) {
    String myArgs[] = Commandline.translateCommandline("-a hello -b world -c \"Hello world\"");
    for (String arg:myArgs)
        System.out.println(arg);
}

魔法类 命令行 是<的一部分EM>蚁。所以你要么必须把ant放在类路径上,要么只需要使用Commandline类,因为使用的方法是静态的。

The magic class Commandline is part of ant. So you either have to put ant on the classpath or just take the Commandline class as the used method is static.

这篇关于将包含命令行参数的字符串拆分为Java中的String []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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