这是OptionParser的精简版本
OptionParser.new do |opts|
opts.on('-f', '--format FORMAT', 'output format (text, html, yml, json, xml)') do |format|
options['format'] = format
end
end
这是格式选项的试用版
[16] pry(main)> parse("-f s")
=> {"format"=>" s"}
[17] pry(main)> parse("--format s")
OptionParser::InvalidOption: invalid option: --format s
--format s
为什么不起作用?
答案 0 :(得分:2)
它可能不起作用,因为.parse
方法应该将参数数组作为参数而不是字符串。将OptionParser
放入实际脚本和.parse(ARGV)
中之后,长样式切换的--format s
和--format==s
变体都应该起作用。
opt.rb脚本:
require 'optparse'
options = {}
parser = OptionParser.new do |opts|
opts.on('-f', '--format FORMAT', 'output format (text, html, yml, json, xml)') do |format|
options['format'] = format
end
end
parser.parse(ARGV)
p options
用法:
~ ruby opt.rb -f s
{"format"=>"s"}
~ ruby opt.rb --format s
{"format"=>"s"}
~ ruby opt.rb --format=s
{"format"=>"s"}
答案 1 :(得分:2)
手动调用parse
时,您需要传入t
,它不是脚本名称后面的所有字符串,而是拆分数组:
public GameObject[] objectSpawns;
public float speed = 1.19f;
Vector3 pointA = new Vector3(3.49f, 29.8f, -41.2f);
Vector3 pointB = new Vector3(13.91f, 29.8f, -41.2f);
void Start()
{
PingPongAllObjects();
}
void PingPongAllObjects()
{
for (int i = 0; i < objectSpawns.Length; i++)
{
StartCoroutine(PingPongEachObject(objectSpawns[i].transform, pointA, pointB));
}
}
IEnumerator PingPongEachObject(Transform targetObject, Vector3 PosA, Vector3 PosB)
{
while (true)
{
//Move to B and wait for the Move to finish
yield return moveToX(targetObject, PosB, speed);
//Move to A and wait for the Move to finish
yield return moveToX(targetObject, PosA, speed);
}
}
IEnumerator moveToX(Transform targetObject, Vector3 toPosition, float speed)
{
float startTime;
// Total distance between the markers.
float journeyLength;
startTime = Time.time;
//Get the current position of the object to be moved
Vector3 startPos = targetObject.position;
// Calculate the journey length.
journeyLength = Vector3.Distance(startPos, toPosition);
if (startPos == toPosition)
yield break;
while (true)
{
// Distance moved = time * speed.
float distCovered = (Time.time - startTime) * speed;
// Fraction of journey completed = current distance divided by total distance.
float fracJourney = distCovered / journeyLength;
// Set our position as a fraction of the distance between the markers.
targetObject.position = Vector3.Lerp(startPos, toPosition, fracJourney);
//Exit if lerp time reaches 1
if (fracJourney >= 1)
yield break;
yield return null;
}
}
因此,如果我们传递这些格式进行解析,则可以正确解析选项:
ARGV