Skip to content

Fix #4005 command-line options help is incomplete #4018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 26, 2018

Conversation

fedor-shi
Copy link
Contributor

  • added default value output
  • added choices values output

- added default value output
- added choices values output
Copy link
Contributor

@Blaisorblade Blaisorblade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this already looks much closer to what scalac -help does, and it's much simpler than I expected!

I have a few comments on making the code even simpler, but they're small things.

I also checked by hand you signed the CLA (https://www.lightbend.com/contribute/cla/scala/check/ioganegambaputifonguser), don't worry if the PR check for the CLA is stuck.

def defaultValue: String = implicitly[ClassTag[T]] match {
case StringTag => default.asInstanceOf[String]
case IntTag => default.asInstanceOf[Int].toString
case _ => ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both branches could be default.toString, but it seems you want to exclude defaults that aren't String and Int? Could you explain why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that only for these tag classes there are meaningful default values. For example for the -version there is default: Boolean == false, what does not make sense.

help.append(s"${format(s.name)} ${s.description}")
help.append(formatSettings("Default", s.defaultValue))
help.append(formatSettings("Choices", s.legalChoices))
help.toString
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this code already using the s formatter, what about just one expression?
s"${format(s.name)} ${s.description}${formatSettings("Default", s.defaultValue)}${formatSettings("Choices", s.legalChoices)}"? You can put arbitrary expressions inside ${} 🙂

@@ -71,7 +71,14 @@ object CompilerCommand extends DotClass {
val ss = (ctx.settings.allSettings filter cond).toList sortBy (_.name)
val width = (ss map (_.name.length)).max
def format(s: String) = ("%-" + width + "s") format s
def helpStr(s: Setting[_]) = s"${format(s.name)} ${s.description}"
def formatSettings(name: String, value: String) = if (value.nonEmpty) format("\n") ++ s" $name: $value." else ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, why do we need to use format on \n? I'm probably being slow (or rushed)...
And expressions can go inside the s formatter...
Any chance we can use s"\n${format(name)} ${format(value)}"? That's closer to what we do in helpStr, and hopefully clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use format("\n") as a hack to start the second line with an empty first column for Default or Choices
screen shot 2018-02-20 at 16 12 08

def isDefaultIn(state: SettingsState) = valueIn(state) == default
def isDefaultIn(state: SettingsState): Boolean = valueIn(state) == default

def defaultValue: String = implicitly[ClassTag[T]] match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make it an inner method of def helpStr. Also you don't need a cast here:

def defaultValue(setting: Setting[_]): String = setting.default match {
  case _: Int | _: String => setting.default.toString
  case _ => ""
}

@@ -71,7 +71,15 @@ object CompilerCommand extends DotClass {
val ss = (ctx.settings.allSettings filter cond).toList sortBy (_.name)
val width = (ss map (_.name.length)).max
def format(s: String) = ("%-" + width + "s") format s
def helpStr(s: Setting[_]) = s"${format(s.name)} ${s.description}"
def formatSettings(name: String, value: String) = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can make it an inner method of def helpStr

}
def helpStr(s: Setting[_]) =
s"${format(s.name)} ${s.description} ${formatSettings("Default", s.defaultValue)} ${formatSettings("Choices", s.legalChoices)}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ioganegambaputifonguser Thanks for the changes, I think they address most of my suggestions :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Blaisorblade you are welcome. also added comments to make it more clear.

@@ -88,7 +88,7 @@ object Settings {
state.update(idx, x.asInstanceOf[T])
}

def isDefaultIn(state: SettingsState) = valueIn(state) == default
def isDefaultIn(state: SettingsState): Boolean = valueIn(state) == default

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we don’t need to add this type annotation, no? Otherwise, LGTM.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the Boy Scout Rule and added this small thing :) Is it true that the public methods have to have the return type annotation? Please correct me if I am not right.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see! Fine with me.

@fedor-shi
Copy link
Contributor Author

@allanrenucci done. Could you please take a look?

Copy link
Contributor

@allanrenucci allanrenucci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM. Thanks @ioganegambaputifonguser!

@@ -71,7 +73,23 @@ object CompilerCommand extends DotClass {
val ss = (ctx.settings.allSettings filter cond).toList sortBy (_.name)
val width = (ss map (_.name.length)).max
def format(s: String) = ("%-" + width + "s") format s
def helpStr(s: Setting[_]) = s"${format(s.name)} ${s.description}"
def helpStr(s: Setting[_]) = {
def defaultValue(setting: Setting[_]) = setting.default match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need a parameter:

def defaultValue = s.default match { ...

else
""
}
s"${format(s.name)} ${s.description} ${formatSetting("Default", defaultValue(s))} ${formatSetting("Choices", s.legalChoices)}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extraneous spaces:

s"${format(s.name)} ${s.description}${formatSetting("Default", defaultValue(s))}${formatSetting("Choices", s.legalChoices)}"

@allanrenucci allanrenucci merged commit fcd422a into scala:master Feb 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants