CARVIEW |
Select Language
HTTP/2 200
date: Wed, 23 Jul 2025 07:44:43 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
etag: W/"a270a4878fc2caf25d0d32f2a0b62a14"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=L10s9NS4y41r54D1sF6OFwyzTr7Dn7ILjydAym9qHE7gjFX8Kyi7gIq4s%2Fc2V%2BarV07SlQTTSE6CVVBwSJAmqQG8ep785EGXGi18nTYgKhTrzJWbJ3MeEEXa77xYxCguJPkOPaolk1wbIaqn9fsqcvtQ3sSwpyREw3vCmQRUL7zzgzNPNWQ3U7OlP9tCMQXPYb58zfdlsQt3milriN0wYV9KsrZbVrrwSGds6dzl5zrVNDOHAf994gV%2FAYsNjzK%2FvgUrWmA2Jbe3Ys0ntbAuvQ%3D%3D--jL6u820NdAlzWFGB--hP4Iu8J1G%2BD8FPvsbH%2FXkw%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.611636051.1753256683; Path=/; Domain=github.com; Expires=Thu, 23 Jul 2026 07:44:43 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Thu, 23 Jul 2026 07:44:43 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: C680:3644F0:66D66E:81023D:688092EA
gist:246481 · GitHub
Show Gist options
Save timperrett/246481 to your computer and use it in GitHub Desktop.
{{ message }}
Instantly share code, notes, and snippets.
Created
December 1, 2009 17:44
-
Star
1
(1)
You must be signed in to star a gist -
Fork
1
(1)
You must be signed in to fork a gist
-
Save timperrett/246481 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.harnly.aaron.options | |
import scala.collection.mutable.{ListBuffer} | |
// Base class for options. | |
// These are things that get listed when we ask for help, | |
// and optionally can accept string arguments & perform some kind of action, | |
// usually mutating a var. | |
case class OptionDefinition( | |
canBeInvoked: Boolean, | |
shortopt: String, | |
longopt: String, | |
description: String, | |
action: String => Unit, | |
gobbleNextArgument: Boolean | |
) | |
// ----- Some standard option types --------- | |
class SeparatorDefinition( | |
description: String | |
) extends OptionDefinition(false,null,null,description, {a: String => {}}, false) | |
class ArgOptionDefinition( | |
shortopt: String, | |
longopt: String, | |
description: String, | |
action: String => Unit | |
) extends OptionDefinition(true,shortopt, longopt, description, action, true) | |
class IntArgOptionDefinition( | |
shortopt: String, | |
longopt: String, | |
description: String, | |
action: Int => Unit | |
) extends OptionDefinition(true,shortopt, longopt, description, {a: String => action(a.toInt)}, true) | |
class DoubleArgOptionDefinition( | |
shortopt: String, | |
longopt: String, | |
description: String, | |
action: Double => Unit | |
) extends OptionDefinition(true,shortopt, longopt, description, {a: String => action(a.toDouble)}, true) | |
class BooleanArgOptionDefinition( | |
shortopt: String, | |
longopt: String, | |
description: String, | |
action: Boolean => Unit | |
) extends OptionDefinition(true,shortopt, longopt, description, { a: String => | |
val boolValue = a.toLowerCase match { | |
case "true" => true | |
case "false" => false | |
case "yes" => true | |
case "no" => false | |
case "1" => true | |
case "0" => false | |
case _ => throw new IllegalArgumentException("Expected a string I can interpret as a boolean") | |
} | |
action(boolValue)}, true) | |
class FlagOptionDefinition( | |
shortopt: String, | |
longopt: String, | |
description: String, | |
action: => Unit | |
) | |
extends OptionDefinition(true,shortopt, longopt, description, { a: String => action }, false) | |
// OptionParser is instantiated within your object, | |
// set up by an (ordered) sequence of invocations of #on, | |
// and then handed the command-line arguments. | |
case class OptionParser(warnOnUnknownArgument: Boolean){ | |
def this() = this(true) | |
val options = new ListBuffer[OptionDefinition] | |
// -------- Defining options --------------- | |
def add(option: OptionDefinition){ | |
options += option | |
} | |
def on(shortopt: String, longopt: String, description: String, action: String => Unit) = | |
add(new ArgOptionDefinition(shortopt, longopt, description, action)) | |
def on(shortopt: String, longopt: String, description: String, action: => Unit) = | |
add(new FlagOptionDefinition(shortopt, longopt, description, action)) | |
def separator(description: String) = | |
add(new SeparatorDefinition(description)) | |
def help(shortopt: String, longopt: String) = | |
add(new FlagOptionDefinition(shortopt, longopt, "Show this help", { this.showUsage; exit })) | |
// we have to give these typed options separate names, because of &^@$! type erasure | |
def onInt(shortopt: String, longopt: String, description: String, action: Int => Unit) = | |
add(new IntArgOptionDefinition(shortopt, longopt, description, action)) | |
def onDouble(shortopt: String, longopt: String, description: String, action: Double => Unit) = | |
add(new DoubleArgOptionDefinition(shortopt, longopt, description, action)) | |
def onBoolean(shortopt: String, longopt: String, description: String, action: Boolean => Unit) = | |
add(new BooleanArgOptionDefinition(shortopt, longopt, description, action)) | |
// -------- Getting usage information --------------- | |
def descriptions: Seq[String] = options.map( opt => opt match { | |
case x if ! x.canBeInvoked => x.description | |
case x if x.gobbleNextArgument => x.shortopt + " <value> or " + x.longopt + " <value>: " + x.description | |
case _ => opt.shortopt + " or " + opt.longopt + ": " + opt.description | |
}) | |
def usage: String = descriptions.mkString("\n") | |
def showUsage = Console.err.println(usage) | |
// -------- Parsing --------------- | |
def parse(args: Seq[String]){ | |
var i = 0 | |
while (i < args.length) { | |
val arg = args(i) | |
val matchingOption = options.find( opt => | |
opt.canBeInvoked | |
&& (arg == opt.shortopt || arg == opt.longopt) | |
) | |
matchingOption match { | |
case None => if (warnOnUnknownArgument) System.err.println("WARNING: Unknown argument '" + arg + "'") | |
case Some(option) => | |
val argToPass = if (option.gobbleNextArgument) { | |
i += 1; args(i) | |
} else | |
"" | |
option.action.apply(argToPass) | |
} | |
i += 1 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can’t perform that action at this time.