Skip to content

Commit

Permalink
Removed ArgumentParser dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhvorost committed Apr 10, 2021
1 parent 12d0b0e commit e69a0b3
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 51 deletions.
16 changes: 0 additions & 16 deletions Package.resolved

This file was deleted.

5 changes: 1 addition & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ let package = Package(
.library(name: "DLog", targets: ["DLog"]),
.executable(name: "NetConsole", targets: ["NetConsole"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.3.1"),
],
targets: [
.target(name: "DLog"),
.target(name: "NetConsole", dependencies: [ .product(name: "ArgumentParser", package: "swift-argument-parser")]),
.target(name: "NetConsole"),
.testTarget(name: "DLogTests", dependencies: ["DLog"]),
],
swiftLanguageVersions: [.v5]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![codecov](https://codecov.io/gh/ikhvorost/DLog/branch/master/graph/badge.svg?token=DJLKDA9W1Q)](https://codecov.io/gh/ikhvorost/DLog)
[![swift doc coverage](https://img.shields.io/badge/swift%20doc-100%25-f39f37)](https://github.com/SwiftDocOrg/swift-doc)

<p align="center"><img src="Images/dlog.png" alt="Logger for Swift"></p>
<p align="center"><img src="Images/dlog.png" alt="DLog: Modern logger with pipelines for Swift"></p>

DLog supports emoji and colored text output, oslog, pipelines, filtering, scopes, intervals and more.

Expand Down
75 changes: 75 additions & 0 deletions Sources/NetConsole/Arguments.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// Arguments.swift
//
// NetConsole for DLog
//
// Created by Iurii Khvorost <[email protected]> on 2021/04/09.
// Copyright © 2021 Iurii Khvorost. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

class Arguments {
private var arguments = [String : String]()

init() {
var key: String?
for param in CommandLine.arguments {
if param.hasPrefix("-") {
if key != nil {
arguments[key!] = ""
}

key = param

// Last
if param == CommandLine.arguments.last {
arguments[key!] = ""
}
}
else {
if key != nil {
arguments[key!] = param
key = nil
}
}
}
}

func stringValue(forKeys keys: [String], defaultValue: String) -> String {
for key in keys {
if let value = arguments[key], !value.isEmpty {
return value
}
}
return defaultValue
}

func boolValue(forKeys keys: [String], defaultValue: Bool = false) -> Bool {
for key in keys {
if arguments[key] != nil {
return true
}
}
return false
}

}
62 changes: 32 additions & 30 deletions Sources/NetConsole/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
// THE SOFTWARE.
//


import Foundation
import ArgumentParser


class Service : NSObject {

Expand Down Expand Up @@ -144,33 +141,38 @@ extension Service : StreamDelegate {
}
}

let info = "NetConsole for DLog v.1.0"
// Arguments

struct NetConsole: ParsableCommand {

static let configuration = CommandConfiguration(abstract: info)

@Option(
name: [.long, .short],
help: "The name by which the service is identified to the network. The name must be unique and by default it equals \"DLog\". If you pass the empty string (\"\"), the system automatically advertises your service using the computer name as the service name.")
var name: String?

@Flag(
name: [.long, .short],
help: "Clear a terminal on new connection.")
var autoClear = false

@Flag(
name: [.long, .short],
help: "Enable debug messages.")
var debug = false

func run() throws {
print(info)

let _ = Service(name: name ?? "DLog", debug: debug, autoClear: autoClear)
RunLoop.current.run()
}
let arguments = Arguments()

let overview = "NetConsole v.1.1"

var help = arguments.boolValue(forKeys: ["--help", "-h"])
guard !help else {
print(
"""
OVERVIEW: \(overview)
USAGE: netconsole [--name <name>] [--auto-clear] [--debug]
OPTIONS:
-n, --name <name> The name by which the service is identified to the network. The name must be unique and by default it equals
"DLog". If you pass the empty string (""), the system automatically advertises your service using the computer
name as the service name.
-a, --auto-clear Clear a terminal on new connection.
-d, --debug Enable debug messages.
-h, --help Show help information.
"""
)
exit(EXIT_SUCCESS)
}

NetConsole.main()
var name = arguments.stringValue(forKeys: ["--name", "-n"], defaultValue: "DLog")
var autoClear = arguments.boolValue(forKeys: ["--auto-clear", "-a"])
var debug = arguments.boolValue(forKeys: ["--debug", "-d"])

let _ = Service(name: name, debug: debug, autoClear: autoClear)

print(overview)

RunLoop.main.run()

0 comments on commit e69a0b3

Please sign in to comment.