如何使选项接受任意数量的参数(How do I make an option accept an arbitrary number of arguments)
我正在尝试编写一个shell脚本,其中包含在每次运行中使用不同数量值的选项,例如
运行1:
./myscript --input <file1> <file2> --output <folder1>
运行2:
./myscript --input <file1> <file2> <file3> --output <folder1> <folder2>
我尝试使用getopts如下:
while getopts ":i:o:" opt; do case $opt in i) echo "treatment files are: $OPTARG" >&2 infiles="${OPTARG}" ;; o) echo "control files are: $OPTARG" >&2 outdir="${OPTARG}" ;; \?) echo "Invalid option: -$OPTARG" >&2 exit 1 ;; :) echo "Option -$OPTARG requires an argument." >&2 exit 1 ;; esac done
但是,我发现为“-i”选项输入可变数量值的唯一方法是引用(“file1 file2”),这阻止我/用户使用命令行的自动完成功能来帮助生成文件路径。 此选项似乎也不允许我使用“--input”语法来传递选项。
有人可以帮忙吗?
非常感谢。
I am trying to write a shell script with options that take different numbers of values in each run, e.g.
Run 1:
./myscript --input <file1> <file2> --output <folder1>
Run 2:
./myscript --input <file1> <file2> <file3> --output <folder1> <folder2>
I tried using getopts as follows:
while getopts ":i:o:" opt; do case $opt in i) echo "treatment files are: $OPTARG" >&2 infiles="${OPTARG}" ;; o) echo "control files are: $OPTARG" >&2 outdir="${OPTARG}" ;; \?) echo "Invalid option: -$OPTARG" >&2 exit 1 ;; :) echo "Option -$OPTARG requires an argument." >&2 exit 1 ;; esac done
However, the only way I found to input variable amounts of values for the "-i" option is by quotation ("file1 file2"), which prevents me/the user to use the autocomplete function of the command line to help generating the file path. Also this option does not seem allow me to use "--input" syntax to pass the options.
Anybody can help?
Thanks a lot.
原文:https://stackoverflow.com/questions/50517122
最满意答案
你有正确的想法,但Swift的
String
和Cocoa的NSString
是两种不同的类型。 你没有意识到这一点的原因是,感谢一些魔术,Swift很乐意在String
上调用NSString
方法来让你的生活更轻松。 在NSString
而不是String
上进行扩展,它应该在两个上下文中都有效。You have the right idea, but Swift's
String
and Cocoa'sNSString
are two different types. The reason you aren't aware of this is that thanks to some magic, Swift is happy to callNSString
methods onString
to make your life easier. Make your extension onNSString
instead ofString
and it should work in both contexts.
相关问答
更多-
确保在swift类名之前放置@objc。 @objc myclassname { ... } 另外添加 @class myclassname; 在你想访问Swift类的obj-c头文件中 Make sure to put @objc before the swift class name. @objc myclassname { ... } Also add @class myclassname; in your obj-c header file where you want to access ...
-
如何在Swift项目中使用Objective-C Cocoapods?(How to use Objective-C CocoaPods in a Swift Project)[2022-09-08]
您的问题的基本答案是是的,您可以使用CocoaPods构建的Objective-C代码。 更重要的问题是“如何使用这样的库?” 这个问题的答案取决于use_frameworks! 您的Podfile标志: 让我们想象你想要使用名为CoolObjectiveCLib Objective-C pod。 如果您的pod文件使用use_frameworks! 旗: // Podfile use_frameworks! pod 'CoolObjectiveCLib' 那么你不需要添加任何桥头文件。 您需要的一切都是 ... -
很简单。 只需在swift类之前声明@objc public 。 喜欢这个 @objc public class YourSwiftClass : NSObject { } 现在在目标c类中使用#import "YourProjectName-Swift.h" 。 您将收到未找到所述文件的错误。 别担心,只需运行一次项目。 在运行时单击该文件时,您将看到“YourProjectName-Swift.h”的内容。 现在你可以访问Swift类的任何方法。 Very Simple. Just declare ...
-
定义Swift和Objective-C类的扩展(Defining an extension for both the Swift and Objective-C classes)[2023-10-02]
你有正确的想法,但Swift的String和Cocoa的NSString是两种不同的类型。 你没有意识到这一点的原因是,感谢一些魔术,Swift很乐意在String上调用NSString方法来让你的生活更轻松。 在NSString而不是String上进行扩展,它应该在两个上下文中都有效。 You have the right idea, but Swift's String and Cocoa's NSString are two different types. The reason you aren't ... -
原来我只是缺少类/函数声明中的public修饰符: public class SomeClass : NSObject Turns out I was just missing the public modifier on the class/function declarations: public class SomeClass : NSObject
-
呃,打了几个小时头,然后终于发布了这个问题,几分钟后我找到了答案: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_82 标题下:“将Swift导入Objective-C” Ugh, after smacking my head for a few hours the ...
-
是否可以在Objective-C中调用Swift便捷初始化程序(Is it possible to call Swift convenience initializer in Objective-C)[2021-12-16]
查看使用Swift与Cocoa和Objective-C(Swift 2.2) - 混合和匹配 。 它似乎归结为什么 public你的便利初始化,和 将XCode生成的头文件[YourProductModuleName]-Swift.h导入到您的代码中 Check out Using Swift with Cocoa and Objective-C (Swift 2.2) - Mix and Match. What it seems to come down to is Making your conveni ... -
如何从同一个故事板访问Objective-C和Swift类?(How to access both Objective-C and Swift classes from same storyboard?)[2023-12-11]
似乎Xcode 6.0(6A215l)需要在可以访问Swift类之前将Storyboard文件升级到更新的版本。 如果使用具有相同名称的Swift类替换Objective-C类,则需要打开Storyboard,编辑自定义类的名称(如删除最后一个字母),保存Storyboard,然后重命名该类。 这将强制将Storyboard文件升级到更新的版本,并使用新的Xcode正确编写自定义类键,如下所示: 版本信息差异: UIButton自定义类定义diff: 在Storyboard文件中进行这些更改后,警告似乎消失 ... -
我相信没有办法从Swift访问Objective-C实例变量。 只有Objective-C属性才会映射到Swift属性。 I believe that there is no way to access Objective-C instance variables from Swift. Only Objective-C properties get mapped to Swift properties.
-
Swift只支持与Objective-C的桥接。 您需要将任何CPP代码/声明移动到.mm文件,例如: foo.h中 #import
@interface Foo : NSObject - (void)bar; @end Foo.mm #import "Foo.h" #import @interface Foo() { std::vector _array; } @end @implementation F ...