Skip to content

Commit

Permalink
tools: support long parameters, change parsesymbols signature
Browse files Browse the repository at this point in the history
  • Loading branch information
kosyloa committed Dec 15, 2023
1 parent f0dca44 commit c0adffe
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
</CommandLineArgument>
<CommandLineArgument
argument = " qds post :6666"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "Dump"
Expand All @@ -72,12 +72,12 @@
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "qds &quot;connect &quot;dxlink:wss://demo.dxfeed.com/dxlink-ws&quot; Quote AAPL&quot; -p dxfeed.experimental.dxlink.enable=true,scheme=ext:resource:dxlink.xml "
argument = "qds &quot;connect &quot;dxlink:wss://demo.dxfeed.com/dxlink-ws&quot; Quote AAPL&quot; --properties dxfeed.experimental.dxlink.enable=true,scheme=ext:resource:dxlink.xml "
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "Connect demo.dxfeed.com:7300 candle AAPL -f 2012-05-26-14:15:00"
isEnabled = "NO">
argument = "Connect demo.dxfeed.com:7300 quote AAPL,IBM"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = " Dump demo.dxfeed.com:7300 timeandsale,quote,profile,order,trade IBM,AAPL,ETH/USD:GDAX -p dxfeed.wildcard.enable=true,one_more_prop=abc -t test_tape_ios.txt[format=text] -q"
Expand Down
73 changes: 50 additions & 23 deletions Samples/Tools/Arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,43 @@ enum ArgumentParserException: Error {
/// The command name is included in the resulting array
/// and therefore 0 is not the first parameter. It is command name
class Arguments {
struct ArgParameter {
let shortName: String?
let longName: String?

public static func == (lhs: ArgParameter,
rhs: String) -> Bool {
if let shortName = lhs.shortName {
if "-\(shortName)" == rhs {
return true
}
}
if let longName = lhs.longName {
if "--\(longName)" == rhs {
return true
}
}
return false
}
}

private let allParameters: [String]
private let namelessParameters: [String]

private let propertiesParameter = ArgParameter(shortName: "p", longName: "properties")
private let quiteParameter = ArgParameter(shortName: "q", longName: "quite")
private let forceStreamParameter = ArgParameter(shortName: nil, longName: "force-stream")
private let tapeParameter = ArgParameter(shortName: "t", longName: "tape")
private let fromTimeParameter = ArgParameter(shortName: "f", longName: "from-time")
private let sourceParameter = ArgParameter(shortName: "s", longName: "source")

public lazy var properties: [String: String] = {
var properties = [String: String]()
if let propIndex = allParameters.firstIndex(of: "-p") {
if let propIndex = allParameters.firstIndex(where: { return propertiesParameter == $0 }) {
allParameters[propIndex + 1].split(separator: ",").forEach { substring in
let prop = substring.split(separator: "=")
if prop.count == 2 {
properties[String(prop.first!)] = String(prop.last!)
properties[String(prop.first!)] = String(prop.last!)
} else {
print("Wrong property \(prop)")
}
Expand All @@ -33,39 +60,38 @@ class Arguments {
return properties
}()

public lazy var isQuite: Bool = {
if let isQuiteIndex = allParameters.firstIndex(of: "-q") {
private func value(for param: ArgParameter) -> Bool {
if allParameters.firstIndex(where: { param == $0 }) != nil {
return true
}
return false
}

private func value(for param: ArgParameter) -> String? {
if let tapeIndex = allParameters.firstIndex(where: { param == $0 }) {
return allParameters[tapeIndex + 1]
}
return nil
}

public lazy var isQuite: Bool = {
return value(for: quiteParameter)
}()

public lazy var isForceStream: Bool = {
if let isForceStream = allParameters.firstIndex(of: "--force-stream") {
return true
}
return false
return value(for: forceStreamParameter)
}()

public lazy var tape: String? = {
if let tapeIndex = allParameters.firstIndex(of: "-t") {
return allParameters[tapeIndex + 1]
}
return nil
return value(for: tapeParameter)
}()

public lazy var time: String? = {
if let tapeIndex = allParameters.firstIndex(of: "-f") {
return allParameters[tapeIndex + 1]
}
return nil
return value(for: fromTimeParameter)
}()

public lazy var source: String? = {
if let tapeIndex = allParameters.firstIndex(of: "-s") {
return allParameters[tapeIndex + 1]
}
return nil
return value(for: sourceParameter)
}()

public lazy var qdsCommandLine: [String]? = {
Expand Down Expand Up @@ -124,11 +150,12 @@ Cmd \(cmd) contains not enough \(cmd.count - 1) arguments. Expected \(requiredNu
}
}

public func parseSymbols(at index: Int) -> [Symbol] {
if namelessParameters.count <= index {
public func parseSymbols() -> [Symbol] {
let symbolsPosition = 3
if namelessParameters.count <= symbolsPosition {
return [WildcardSymbol.all]
}
let symbols = namelessParameters[index]
let symbols = namelessParameters[symbolsPosition]
if symbols.lowercased() == "all" {
return [WildcardSymbol.all]
}
Expand Down
2 changes: 1 addition & 1 deletion Samples/Tools/ConnectTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Sample: connect demo.dxfeed.com:7300 Quote AAPL
}

subscription.createSubscription(address: arguments[1],
symbols: arguments.parseSymbols(at: 3),
symbols: arguments.parseSymbols(),
types: arguments.parseTypes(at: 2),
role: arguments.isForceStream ? .streamFeed : .feed,
listeners: listeners,
Expand Down
2 changes: 1 addition & 1 deletion Samples/Tools/DumpTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class DumpTool: ToolsCommand {

func execute() {
let address = arguments[1]
let symbols = arguments.parseSymbols(at: 3)
let symbols = arguments.parseSymbols()

isQuite = arguments.isQuite

Expand Down
2 changes: 1 addition & 1 deletion Samples/Tools/LatencyTestTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class LatencyTestTool: ToolsCommand {
let listener = LatencyEventListener()

subscription.createSubscription(address: address,
symbols: arguments.parseSymbols(at: 3),
symbols: arguments.parseSymbols(),
types: types,
role: arguments.isForceStream ? .streamFeed : .feed,
listeners: [listener],
Expand Down
2 changes: 1 addition & 1 deletion Samples/Tools/PerfTestTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class PerfTestTool: ToolsCommand {
let listener = PerfTestEventListener()

subscription.createSubscription(address: address,
symbols: arguments.parseSymbols(at: 3),
symbols: arguments.parseSymbols(),
types: arguments.parseTypes(at: 2),
role: arguments.isForceStream ? .streamFeed : .feed,
listeners: [listener],
Expand Down

0 comments on commit c0adffe

Please sign in to comment.